Table of Contents
- Introduction to Classes and Objects
- Defining a Class
- Fields (Instance Variables)
- Methods in a Class
- Creating Objects
- Memory Allocation for Objects
- Accessing Members using Objects
- Multiple Objects of the Same Class
- Anonymous Objects
- Summary and Best Practices
1. Introduction to Classes and Objects
Classes and objects are the foundation of object-oriented programming in Java. A class serves as a blueprint for creating objects, while an object is an instance of a class. Classes define properties and behaviors, and objects represent real-world entities with those properties and behaviors.
Understanding how classes and objects work is essential for building modular, maintainable, and scalable Java applications.
2. Defining a Class
In Java, a class is defined using the class
keyword. It may contain fields (variables), methods, constructors, blocks, and nested classes or interfaces.
Syntax:
class ClassName {
// Fields
// Methods
}
Example:
class Car {
String color;
int speed;
void drive() {
System.out.println("Driving at " + speed + " km/h");
}
}
In the example above, Car
is a class with two fields (color
and speed
) and one method drive()
.
3. Fields (Instance Variables)
Fields, also called instance variables, are variables declared inside a class but outside any method. Each object of the class gets its own copy of the instance variables.
class Student {
String name;
int age;
}
Each Student
object will have its own name
and age
.
4. Methods in a Class
Methods define the behavior of the class. They are blocks of code that perform specific actions and can access the class’s fields.
class Student {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Methods can be parameterized, return values, or be void (return nothing). They help encapsulate the logic that operates on the class’s data.
5. Creating Objects
An object is created using the new
keyword followed by the class constructor.
Student s1 = new Student();
This statement creates a new Student
object and assigns it to reference variable s1
.
Each time you use new
, a new object is created in memory with its own copy of instance variables.
6. Memory Allocation for Objects
When a Java object is created:
- Memory is allocated for instance variables in the heap memory.
- Local variables and references (like
s1
in the above example) are stored in stack memory. - The object’s methods are stored in method area.
Each object has its own copy of non-static fields, ensuring isolation and independence between instances.
7. Accessing Members using Objects
Object members (fields and methods) are accessed using the dot operator (.
).
Student s1 = new Student();
s1.name = "Alice";
s1.age = 22;
s1.displayInfo(); // Output: Name: Alice, Age: 22
You can modify field values or call methods through object references.
8. Multiple Objects of the Same Class
You can create multiple objects from the same class, each with their own field values.
Student s1 = new Student();
s1.name = "Alice";
s1.age = 22;
Student s2 = new Student();
s2.name = "Bob";
s2.age = 25;
Each object maintains its own state. Changing one object’s data does not affect another.
9. Anonymous Objects
An anonymous object is created without a reference variable. These are typically used for a single-use purpose, such as method invocation.
new Student().displayInfo(); // Creates a Student object and calls displayInfo()
Although convenient, anonymous objects cannot be reused since there’s no reference to access them again.
Use anonymous objects when object reuse is unnecessary, such as in testing or method chaining.
10. Summary and Best Practices
Summary
- A class defines the structure (fields) and behavior (methods) for its objects.
- An object is an instance of a class created using the
new
keyword. - Objects are stored in heap memory, while their references are on the stack.
- Each object maintains its own state through its fields.
- Methods define what an object can do and can be invoked through object references.
Best Practices
- Use meaningful class and object names (e.g.,
Student
,Employee
,BankAccount
). - Keep fields private and use getters/setters to enforce encapsulation.
- Avoid using unnecessary public fields as it breaks encapsulation.
- Prefer creating named objects unless there’s a strong reason for anonymity.
- Structure classes logically, keeping related data and behavior together.