aspose file tools*
The moose likes Java in General and the fly likes overriding finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "overriding finalize()" Watch "overriding finalize()" New topic
Author

overriding finalize()

Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi....
Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi....
i have seen people override finalize() method. But if its not certain whether this method will be called then what is the use of overriding it. It will make code unpredictable. Can any one give me a real life situation where you need to override finalize() method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

finalize() isn't used often, and there really isn't much reason to use it. If you've written a class with a "close()" or "dispose()" or "cleanup()" method -- something that the user is supposed to call when they are done with an object -- putting a call to that method in a finalize() method is a sensible thing to do just to be safe, but not necessary. Classes like FileWriter, java.awt.Graphics, etc, do what I've described here.


[Jess in Action][AskingGoodQuestions]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
off to the intermediate forum


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Ernest Friedman-Hill:
If you've written a class with a "close()" or "dispose()" or "cleanup()" method -- something that the user is supposed to call when they are done with an object -- putting a call to that method in a finalize() method is a sensible thing to do just to be safe, but not necessary. Classes like FileWriter, java.awt.Graphics, etc, do what I've described here.


I think that, in these cases, an assertion should be added to finalize(). This means that your developer builds will throw an assertion failure if you have forgotten to close something. But assertions are disabled (if you've built things right) in release builds, so your release builds will have the protection of the finaliser to close anything that you forgot.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi....
Thank you for the reply.
Also I have one more doubt regarding finalize(). If in finalize() method this object is reassigned(so garbage collector will not collect the object), why garbage collector does not calls finalize method again.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Ashwin Sharma:
hi....
Thank you for the reply.
Also I have one more doubt regarding finalize(). If in finalize() method this object is reassigned(so garbage collector will not collect the object), why garbage collector does not calls finalize method again.


Do not know the exact reason for this behavior.But a object's finalize method can be called at most once.During the call of finalize method , if you reassign the object to a live thread then even if sometime later it becomes eligible for garbage collection then also the finalize would not be called for that object.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding finalize()