This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize method confusion

 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can't understand these lines can anybody help me........
"In the finalize() method you could write code that passes a reference
to the object in question back to another object, effectively uneligiblizing the object
for garbage collection."
thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finalize method is called before an object is garbage collected. An object is garbage collected when there are no more references to it.
So when the finalize method is called then there are no more references to the object. But if the finalize method introduces a reference (e.g.
by calling something like CentralRegister.register(this);) then the garbage collector will not garbage collect the object.
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont know about CentralRegister.register(this);, can you please elaborate it, if possible provide me a code snippet which passes a reference
to the object in question back to another object........
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is just an example of introducing a reference to the object.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Saurabh,

Below code should help you.



As in the finalize method we are assigning the object back to some object reference variable, so GC will not delete this object.

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic