MY mENU


Thursday 9 February 2012

Finalize() method in java


The Object class provides a callback method, finalize(), that may be invoked on an object when it becomes garbage.

Java provides us a mechanism to run some code just before our object is deleted by the Garbage Collector. This code is located in a method named finalize().

When JVM trying to delete the objects, then some objects refuse to delete if they held any resource. If our object opened up some resources, and we woluld like to close them before our object is deleted. So before Garbage operation we have to clean up the resources which the object held on, that clean up code we have to put in finalize() method.

- We can override finalize() to do cleanup, such as freeing resources. Any code that we put into our Class's overridden finalize() method is not guarunteed to run, so don't provide essential code in that method.

No comments:

Post a Comment