I have an object reference = null;
Hence it is eligible for gc. Before the object is garbage collected, finalise method is called. What if in the finalise method i assign the reference a new object.
If an object doesn't have any valid references any more it can be GC'ed. If a reference to that object is created in the finalize method it will not be GC'ed because
it has a valid reference to 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.
If an object doesn't have any valid references any more it can be GC'ed. If a reference to that object is created in the finalize method it will not be GC'ed because
it has a valid reference to it.
But because objects will only be finalized zero or one times, the object will not be garbage collected or finalized anymore after that, even if there are no more references. This will lead to memory leaks, and therefore you shouldn't do this.
. . . but it may not be GC-ed properly because the finalise() method is not called twice. If you re-create a reference and another attempt at garbage collection is made, finalise() has already been called, so it won't be called again.
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad: