Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize()

 
Mohit Agarwal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic