Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

overriding finalize()

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi....
 
Ashwin Sharma
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
off to the intermediate forum
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic