Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic