• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finalize()

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic