This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Restoring an object in finalize() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Restoring an object in finalize() ?" Watch "Restoring an object in finalize() ?" New topic

Restoring an object in finalize() ?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Part of an explanation concerning garbage collection was given as:
"An object that has been eligible for garbage collection may stop being eligible and return to normal life. This occurs if the call to the finalize() method re-establishes a reference from the active part of the program to the object."
Can someone provide an example of this in a finalize method? I'm not clear how an object once eligble now could become eligible.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
I guess the following code snippet demonstrates the concept:

Test object after creation:Test@17d257
Test object before resurrection:null
Test object after resurrection:Test@17d257

[Blog] [Blogroll] [My Reviews] My Linked In
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Thanks Valentin,
I was thinking that with the statement
in the finalize method that the JVM would see this and wouldn't try to even garbage collect it in the first place after it was set to null in the main. I see now that I was wrong thanks.
subject: Restoring an object in finalize() ?
Similar Threads
Regarding finalize method
finalize() called more than once
Garbage Collection
Garbage collection will be triggered and finalize method will be called ?