aspose file tools*
The moose likes Java in General and the fly likes finalize method confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "finalize method confusion" Watch "finalize method confusion" New topic
Author

finalize method confusion

saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
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

Joined: Oct 25, 2008
Posts: 2700

That is just an example of introducing a reference to the object.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize method confusion