In this tutorial, we will understand the life cycle of object in Java step by step. We should know how objects are born, live their life, and then die at the end.
The life cycle of an object in Java is the complete journey of an object from its creation, its use in the program, and its removal from memory by garbage collector when it is no longer needed. Every object in Java goes through several stages, such as class loading, object creation, initialization, usage, and garbage collection.
Overview of Object Life Cycle in Java
The life cycle of an object in Java consists of the following stages:
- Creation of .class file
- Class loading into memory
- Class initialization
- Object creation (Instantiation)
- Memory allocation
- Constructor execution
- Object usage
- Object becomes eligible for Garbage Collection
- Garbage Collection and destruction
Let us understand each step in detail.
Step 1: Creation of .class File on Disk
As you know that Java is compiled and interpreted language. When you write a Java program, it is saved with a .java extension (Student.java). For example:
public class Student {
int id;
}When you compile it, the Java compiler creates a bytecode file named Student.class on the disk. This .class file contains platform-independent bytecode that can run on any system with a JVM installed.
Step 2: Loading .class File into Memory
After compilation, the JVM loads the .class file into memory using the ClassLoader subsystem. This process is called class loading, which usually happens when:
- You create an object of the class.
- You access a static variable of the class.
- You call a static method of the class.
- You run the program and the JVM starts executing the main() method.
- You explicitly load the class using: Class.forName(“Student”);
Step 3: Looking for Initialized Static Members of Class
After loading the .class file into memory, JVM looks static members of the class in the program. Static members include:
The JVM initializes values to static variables and executing static blocks. Static methods are loaded but executed only when called.
You always remember that all the static members of the class do not belong to any particular instance of the class. It belongs to the class itself and is shared by all the objects created from the class.
Step 4: Object Creation (Instantiation)
When you create an object using the new keyword, the JVM creates the object in heap memory and the reference variable points to that object.
Step 5: Memory Allocation for Object and Reference Variable
When you create an object of the class, JVM always allocates memory for object in heap memory and the reference variable points to that object.
The reference variable that points to that object, is stored into stack memory if declared inside a method. For example:
class Student {
public static void main(String[] args) {
Student s = new Student();
}
}If the reference variable is declared as instance variable in the class, JVM allocates memory for the reference itself within the object on the heap. For example:
class Student {
Student s = new Student();
}Step 6: Constructor Execution
After allocating memory for the object in heap memory, the JVM automatically calls the constructor of the class to initialize the object. The JVM performs the following steps:
- Allocates memory for the object in heap memory.
- Assigns default values to instance variables (0, null, false, etc.).
- Calls the constructor of the class.
- Initializes the object with the values defined in the constructor.
- Returns the reference to the reference variable.
Step 7: Object Usage
After object creation, you can use it to access instance variables and methods. For example:
class Student {
int id = 101;
void display() {
System.out.println("ID: " + id);
}
public static void main(String[] args) {
Student s = new Student(); // object created.
s.display(); // object is used to access method.
}
}Output:
ID: 101
The object remains alive in memory as long as there is at least one reference pointing to it.
Step 8: Object Becomes Eligible for Garbage Collection
When the accessing of field and methods are completed, an object becomes eligible for garbage collection when it is no longer referenced or needed. This usually happens when:
- Reference is set to null.
- Reference goes out of scope.
- Object is reassigned.
Example 1: Null reference
Student s = new Student(); // object created
s = null; // reference set to null
In this example, we have set null to the object reference, meaning the reference variable s does not point to any object in memory. However, the object still exists in heap memory temporarily, but it becomes eligible for garbage collection.
Example 2: Out of scope
void method() {
Student s = new Student();
}
After the complete execution of method, object is eligible to garbage collection.
Step 9: Garbage Collection and Object Destruction
You don’t have to destroy objects yourself. Actually, when the object is no longer in use, JVM calls the garbage collector to destroy all the objects. Garbage Collector (GC) automatically removes unused objects from heap memory. This process is called Garbage Collection. Thus, objects are born, live, and die.
Complete Example Showing Object Life Cycle in Java
Let us take an example in which we will see the complete life cycle of an object in Java.
Example: Object Life Cycle
class Student {
static {
System.out.println("Class Loaded");
}
Student() {
System.out.println("Constructor executed");
}
void display() {
System.out.println("Object is in use");
}
public static void main(String[] args) {
// Creating an object of class.
Student s = new Student();
// Calling method.
s.display();
// Setting reference to null.
s = null;
// Requesting JVM to run Garbage Collector (GC).
System.gc();
System.out.println("Object eligible for GC");
}
}Output:
Class Loaded Constructor executed Object is in use Object eligible for GC
Conclusion
The life cycle of an object in Java mainly consists of:
- Class loading
- Class initialization
- Object creation
- Memory allocation
- Constructor execution
- Object usage
- Garbage collection
The JVM manages memory allocation and garbage collection automatically, which makes Java a powerful language with automatic memory management.
We hope this tutorial has helped you to understand how objects are born, live their life, and then die at the end. Stay tuned with our next tutorial where we will understand a new topic anonymous object in Java.




