aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() 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 "finalize()" Watch "finalize()" New topic
Author

finalize()

Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
On page 439, K&B , in finalize () method you could write a code that passes a reference to the object in Q back to another object thus making it uneligible for Garbage Collection.
Pls. write a code snippet so that i can understand it .

Also i want to if there is a print statement in the finalize() method
but we don't know whether the method will ever be invoked so while choosing the alternative shall we go with the one including the print of the finalize().
There was 1 Q of this in Dan's Exam.

Mohit Agarwal
Would Be SCJP.

"The will to win is worthless if you do not have the will to prepare"
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Regarding your first question.
Here's a class with a finalizer which resurrects itself by renewing its keeper's reference to it:


And here's the keeper:


The keeper is given an instance of Kept. Then it's told to let it go. The Kept instance is then eligible for garbage collection (even though it has a reference to its keeper).

The three calls to System.gc() are an attempt to cause garbage collection occur. On my system it does.

The Kept instance's finalize method gets called which resets the keeper's reference to the Kept instance.
[ October 01, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
Consider Paul's rocket mass heater.
 
subject: finalize()