Table of content:

Objects in Java- Introduction

Object reference in Java

Lifetime of an object in Java

Object finalization in java

Invoking garbage collection explicitly





What are objects in Java ?


An object is an instance of a class and the process of creating an object is called instantiation. An object exhibits the properties and attributes as defined in the class. A class can have multiple objects and every object has its own copy of its instance variables and methods in the heap memory. So in simple words, a class defines the behavior and attributes, whereas the object exhibits that behavior and attributes.


class abc{
int i; //instance variable declaration
public int a(int x){// instance method definition
i=x+1;
System.out.println("Value of i="+i);
return i;
}
}



What is an object reference in Java?


In java, an object is always referenced by a reference variable. The value that denotes an object in java is called an object reference. Such reference values can be stored in variables and used to manipulate the object denoted by the reference value.

class xyz{
public static void main(String args[]){
abc obj1=new abc();//object of the class abc created
abc obj2=new abc();//object of the class abc created
int r=obj1.a(3);
int t=obj2.a(4);
}
}

Here the obj1 and obj2 are the reference variables of the objects of the class 'abc', whereas the 'new' keyword will create an instance/object of the class 'abc' and returns a reference to a new instance of the 'abc' class. The right side of the new operator i.e. abc(); is the constructor of the class. The constructor is used to initialize the instance variables.

The constructor of a class has some properties like:

-Its name should be the same as that of the class
-It does not have any return type
-It is used to initialize the instance variables of the class.


If there is no explicit constructor defined in the class, then a default constructor will be called upon the object creation. Here two objects are created and each object has its own copy of instance variables (fields declared in the class). Both the objects are pointing to their own separate heap instances. In the example above, the value of i will be different for obj1 and obj2.

objects in java



What is the lifetime of an object in Java?


In java, an object is alive until it is pointed or referenced by a reference variable. The moment, an object stops being referred by a reference variable, it becomes eligible for garbage collection. In java, object manipulation can be done with the help of object reference.
if, obj1=obj2;
then the reference variable obj1 will start pointing to instance 2 of the class, whereas instance 1 will be garbage collected. This means the memory will be freed up which was acquired by object 1. Thus the reclaiming of the memory which is acquired by the objects which are no longer referenced by any reference variable is called Garbage collection.

objects reference in java and garbage collection

Objects that are created and accessed by local reference variables i.e. inside a method are eligible for garbage collection when the method terminates. Here, in the below example, obj1 and obj2 are local reference variables created inside a method. Therefore obj1 and obj2 are eligible for garbage collection once the execution of the method concludes.

public class xyz {
public void doSomething() {
abc obj1=new abc();
abc obj2=new abc();
int r=obj1.a(3);
int t=obj2.a(4);
}
public static void main(String[] args) {
xyz x=new xyz();
x.doSomething();
}
}



What is object finalization in java?


In java, the garbage collector gives an object a last chance to undertake any action before the object is destroyed and its memory is reclaimed. A method called finalize() is defined in the Object class. The implementation provided to finalize() method is called finalizer. A subclass can override the finalizer from the object class in order to take more appropriate and specific actions before an object of the subclass is destroyed. The below example shows the class xyz overrides the finalize method from the object class and provides its own finalizer i.e. the implementation to finalize() method of the object class. But from Java version 9, the finalize() method is deprecated.

public class xyz {
abc obj1;
abc obj2;
int r;
int t;
public xyz(){
obj1=new abc();
obj2=new abc();
obj1.a(3);
obj2.a(4);
}
public static void main(String[] args) {
xyz x=new xyz();
}
protected void finalize() throws Throwable {
System.out.println("<---Finalizer--->");
try {
if(obj1!=null) {
System.out.println("Write some code which u want this object to do before it is destroyed");
}
}
finally {
super.finalize();
}
}
}



How to invoke garbage collection explicitly in java?


Using System.gc() , we can request the JVM to invoke garbage collector. The JVM makes efforts to clear the unused objects and make space for new objects in the heap. System.runFinalization() can also be called to run all the pending finalizers before the object is destroyed. runFinalization() can be used to run any pending finalizers for objects eligible for garbage collection. Other explicit methods that can be called are:
freeMemory() :- this will return the amount of free memory in bytes that is available for new objects.
totalMemory():- this will return the total amount of memory in bytes available for new objects along with the memory occupied by the current objects.
Below code-snippet shows free and total memory in bytes before and after garbage collection respectively.

invoking garbage collection in java