Introduction
In Java, everything revolves around objects. An object is an instance of a class that represents a real-world entity with its own properties (state) and behaviors (methods). While a class acts as a blueprint, an object is the actual entity created from that blueprint.
For example, consider a Student class. The class defines common properties such as id, name, and course, along with methods like study() and displayDetails(). When you create a student named Rahul or Neha, each becomes a separate object with its own unique values while sharing the same structure defined by the class.
What is an Object?
An Object is an instance of a class that occupies memory and contains its own data and behavior.
A class only defines the structure, while an object provides actual values for that structure.
Definition:
An object is a runtime instance of a class that contains its own state (fields) and behavior (methods).
For example:
- Class: Car
- Objects: BMW, Audi, Tesla
Each car has different values for color, model, and speed, but they all belong to the same Car class.
Characteristics of an Object
Every object has three important characteristics:
1. State
The state represents the data stored in an object.
Example:
Student
-------
id = 101
name = Rahul
course = Java
2. Behavior
Behavior represents the actions an object can perform.
Examples:
- study()
- displayDetails()
- start()
- stop()
3. Identity
Every object has a unique identity, even if two objects contain the same data.
Student s1 = new Student();
Student s2 = new Student();
Although s1 and s2 may have identical values, they are different objects in memory.
Relationship Between Class and Object
A class is a blueprint, while an object is created from that blueprint.
Class
(Blueprint)
│
▼
new Keyword
│
▼
Object
One class can create many objects.
Example:
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
How to Create an Object
Objects are created using the new keyword.
Syntax
ClassName objectName = new ClassName();
Example:
Student student = new Student();
Here:
- Student → Class name
- student → Object reference
- new → Allocates memory for the object
- Student() → Constructor
Creating Your First Object
class Student {
int id;
String name;
void display() {
System.out.println(id);
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.id = 101;
student.name = "Rahul";
student.display();
}
}
Output
Rahul
Creating Multiple Objects
A class can be used to create multiple objects.
class Student {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.id = 101;
s1.name = "Rahul";
Student s2 = new Student();
s2.id = 102;
s2.name = "Neha";
Student s3 = new Student();
s3.id = 103;
s3.name = "Amit";
s1.display();
s2.display();
s3.display();
}
}
Output
102 Neha
103 Amit
Each object stores its own copy of instance variables.
Accessing Object Members
Use the dot (.) operator to access fields and methods.
student.id = 101;
student.name = "Rahul";
student.display();
The dot operator connects an object with its members.
Anonymous Objects
Sometimes an object is used only once and does not require a reference variable.
Example:
new Student().display();
This is called an anonymous object.
Anonymous objects are commonly used when an object is needed for a single operation.
Object Memory Representation
When an object is created using the new keyword, memory is allocated on the heap.
Stack Memory
-------------------
student
│
▼
Heap Memory
-------------------
Student Object
-------------------
id = 101
name = Rahul
-------------------
The reference variable (student) is stored in the stack and points to the object stored in the heap.
Object Lifecycle
An object’s lifecycle consists of the following stages:
- Class definition.
- Object creation using new.
- Constructor execution.
- Object usage.
- Object becomes unreachable.
- Garbage Collector reclaims memory automatically.
Class
│
▼
new
│
▼
Object Created
│
▼
Used by Program
│
▼
No Reference
│
▼
Garbage Collection
Real-Life Example
Imagine a Car showroom.
The Car class defines:
- Brand
- Model
- Color
- Speed
Objects created from the class:
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
Each object represents a different car.
Another example is a Bank Account.
Class:
BankAccount
Objects:
Rahul's Account
Neha's Account
Amit's Account
Each account has different data but follows the same structure.
Class vs Object
| Class | Object |
|---|---|
| Blueprint or template | Instance of a class |
| Defines properties and methods | Contains actual data |
| Does not store instance data by itself | Occupies memory |
| Created once | Can be created many times |
Advantages of Objects
Objects provide several benefits:
- Represent real-world entities.
- Support code reusability.
- Improve modularity.
- Make programs easier to maintain.
- Enable encapsulation.
- Simplify application development.
- Allow multiple instances with different data.
- Form the basis of Object-Oriented Programming.
Best Practices
Follow these best practices when working with objects:
- Create objects only when necessary.
- Use meaningful object names such as student, employee, or account.
- Initialize objects using constructors whenever possible.
- Keep fields private and access them through methods.
- Reuse objects where appropriate instead of creating unnecessary ones.
- Release references to unused objects so they can be garbage collected.
- Follow Java naming conventions consistently.
Common Mistakes
Beginners often make these mistakes:
- Confusing classes with objects.
- Forgetting to create an object before accessing instance members.
- Attempting to access instance variables without an object.
- Creating unnecessary duplicate objects.
- Using poor object names.
- Forgetting that new allocates memory.
- Misunderstanding reference variables and objects.
- Assuming two objects with the same data are the same object.
Conclusion
A Java object is the runtime instance of a class and the foundation of every object-oriented Java application. Objects represent real-world entities, store their own data, and perform actions through methods. By understanding how objects are created, how they occupy memory, and how they interact with classes, you gain a solid understanding of Object-Oriented Programming.