File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exit of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exit of an object " Watch "exit of an object " New topic
Author

exit of an object

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

How to find whether an object has exited or not?
sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31

How to find whether an object has exited or not?[/B]
Actually what is the meaning of this question.
whether an object has gone out of scope / or gc'd.
If this is the case put some println statement in finalize method
of the object. you will cometo know when it is about to be gc'd.
Sasidhar
sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
Originally posted by amit sanghai:

How to find whether an object has exited or not?

put some thing in its finalize method
This should work.
Sasidhar
sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
Hi, I am sorry I see that the println statement in side finalize is not working .. But I assure you that you can put yr clean up code inside finalize method . And this would definitely be called before the object is garbage collected. Hope I have not confused you too much.
Sasidhar
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi sasi,

Q1) Why does the println() function does not work inside finalize method() ?
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?
sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exit of an object