Do we need to call finalize() method explicitly on an object?
Joined: Aug 10, 2006
Is it necessary to call finalize() method on an abject explicitly or it will be called by garbage Collector automatically whenever the object eligible for Garbage Collection..?
Please answer this.
Joined: May 11, 2005
Any search to google will tell you that its called implicitly by the GC when it determines that the object cannot be reached. I believe putting anything much in it is discouraged, even to the point of never using it.
As Tom said, it is called automatically by the GC. But you can still call it explicitly but that is discouraged and that will not destroy the object from the heap. Also the finalize method has been declared protected in the Object class so that you can make it inaccessible from outside your class and its sub classes. This is done solely to restrict any code outside your class (and its sub classes) from calling the finalize method...
Sandeep Kumar Jakkaraju wrote:I guess we can override the defualt finalize() method !!! .......... may be sometimes useful..to make the members null referenced !!
If you do, always call super.finalize():
I usually only add finalizers to make sure that resources are released, but I always provide means to release resources explicitly. Like Monu's link says, finalizers should only be used as a safety net of sorts; never rely on it, only use it "just in case".