Tata AIG General Insurance Company - Insurer Innovation Award 2024
Java object
1. Java Object Lifecycle
What do we mean by Java Object:
● ~ a collection of data and actions.
● ~ an instance of a class
● ~ have states and behaviors
Car, Bird and Human are considered as Class in Java. Mr. Mofiz is an object of
human and Tata Nano is the object of car
2. Object Creation
Using new keyword is the most common way to create an object in java.
ClassName Obj.Name = new ClassName();
// Human mr_mofiz = new Human();
// Car tata_nano = new Car();
3. Object creation (cont.)
Object creation Statement performs 3 actions:
● Declaration
○ ~ a variable declaration which simply declares to the compiler
○ To refer to an object whose type is Human
● Instantiation
○ new operator instantiates the Human class
● Initialization
○ Human initializes the object
5. “Created” Phase of Java Object Lifecycle
Creation of an Object means
● Allocation of memory
● Calling constructor
● Initializing its properties
When Object is created
● Ready for use
● Lives in memory heap of the JVM
6. “In Use” Phase of Java Object Lifecycle
Any Object that is held by any strong reference is said to be in use
Car tata_nano = new Car(); // Here tata_nano is strong reference to Car Object
● May have multiple strong reference of any object.
● Never approached by GC
Create an Object and forget it without having its reference
7. “Invisible” Phase of Java Object Lifecycle
~ are strong references which are not accessible while the reference and Object
are both are in scope
public void execute() {
Try {
Object obj = new Object();
} catch(Exception e) {
e.printStackTrace();
}
while(true) {
…………….
………….
………….
}
}
8. “Invisible” Phase of Java Object Lifecycle
~referenced by obj is out of scope and is eligible for GC but in fact it lives in the
same stack frame and occupies memory in heap area
~can cause serious memory blockage and there are chances to get
OutOfMemoryException.
Fix: To fix this we have to explicitly set the references to null after using them.
9. “Unreachable” Phase of Java Object Lifecycle
● ~ no more strong references to it exist and it can’t be accessed
● ~ is eligible for GC
● GC is also smart enough to detect circular references and collect them too.
Car tata_nano = new Car(); //line 1
tata_nano = null; //line 2(unreachable)
Car tata_nano = new Car(); //line 1
Owner owner = new Owner(); //line 2
tata_nano.addOwner(owner); //line 3
owner.addVehicle(tata_nano); //line 4
tata_nano = null; //line 5
owner = null; //line 6
10. “Collected” Phase of Java Object Lifecycle
● ~is in the “collected” state when the garbage collector has recognized an
object as unreachable
● ~is just a phase before its deallocation.
● ~ (if ) has finalize method then it is marked for finalization otherwise it directly
moves to finalized state.
11. Finalized Phase of Java Object Lifecycle
● ~is in the “finalized” state if it is still unreachable after it’s finalize method
● ~A finalized object is awaiting de-allocation
● ~to ensure that important resources are freed in a timely manner
● ~To lengthening object lifetimes, finalize methods can increase object size
12. Deallocated Phase of Java Object Lifecycle
● ~is the final step in garbage collection
● If an object is still unreachable after all the above work has occurred, then it is
a candidate for deallocation.
● deallocation occurs is up to the JVM.