Finalize method
Author: Ravi Poswal
In Java, the finalise() method is a protected method for garbage collection. It belongs to the java.lang.Object class. This technique is for cleaning up the item. It is executed before the object is unloaded. This method represses the last wish of the object. A different object may have different wishes.
Key Features of finalize() Method
-
It is called by the garbage collector before destroying an object.
-
It is specified in the class Object.
-
It is typically employed in cleaning tasks.
-
It is only carried out once for every object.
Code Example
class TestFinalize
{
public void finalize()
{
System.out.println("Object is garbage collected");
}
public static void main(String[] args) {
TestFinalize obj1 = new TestFinalize();
TestFinalize obj2 = new TestFinalize();
obj1 = null;
obj2 = null;
System.gc();
}
}
RANREV INFOTECH