Bear Bibeault wrote:Calling finalize() will not cause anything to happen to the object except what is coded within finalize. Besides, if you have a reference to the object in order to be able to call finalize(), it's not eligible for garbage collection in the first place.
finalize() is called as a result of garbage collection, not to initiate it.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Arun Giridharan wrote:Are you tring to say that finalize() method must not be called directly instead must be called through garbage Collection , i don't get IT!!
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized
As you can see, there is no guarantee. For this reason, it is always advisable to perform your cleanup operations in the code itself, instead of putting them in your finalize and relying on the JVM to invoke it.When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
Shinil Mohan wrote:My understanding is if the object is not referenced by any active thread the object will be eligible for Garbage
Shinil Mohan wrote:It’s decided by JVM when to start the garbage collection process.
Shinil Mohan wrote:In the finalize() method we can make the reference of the object to null
Shinil Mohan wrote:to make this object eligible for garbage collection.
Shinil Mohan wrote:Seniors please correct if we are missing something.
luck, db
There are no new questions, but there may be new answers.
Darryl Burke wrote:
Shinil Mohan wrote:In the finalize() method we can make the reference of the object to null
No, you can't.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |