File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding finalize()

 
Ashwin Sharma
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi....
 
Ashwin Sharma
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
off to the intermediate forum
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwin Sharma
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic