Originally posted by amit sanghai:
Hi sasi,
Q1) Why does the println() function does not work inside finalize method() ?
There seems to be some twist here. See the API doc
"protected void finalize()
throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are
no more references to the object. A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup. "
What above means is that finalize is called when the object has already gone out of scope( and at this point of time it is at the mercy of the gc to be started by OS) that is why println method is not working. if we call explicitly the finalize method it is working. No more references means it is out of scope of all methods .
Q2) I meant object is out of scope? I think that you cannot find out whether an object is available for garbage collection. Is that true?
some times you can make finalize method to resurect the ( about to be garbage collected object )object.
to sum it all (as you said ): once an oject is out of scope( of all the reference to it ) we can say it has become ready for garbage collection. but when it is gc'd ( i.e exited ) exactly cannot be known.
sasi