Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finalize Method

 
Bubai Basu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.Objects that have finalize() methods always have their finalize() methods called before the program ends??
2.Objects that have finalize() methods are ever garbage collected ??
Please give me some document on Finalize method.
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) when you write a finalize method in your class you are overriding the finalize method (that i suppose to be empty) of the Object class. In this way when the garbage collector destroy the object it is instructed with the "last will" of your class

2) first of all an object is garbageColected when the Garbage collector choose that the time has come, but also one those two conditions have to true:
a) the object has been set to null (obj = null
b) finalize method of the object has been called;

ciao
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) No, there is no guarantee that finalize() will be called on all objects; only that an object's finalize() will be called when it is collected and its memory is reclaimed.

2) Having a finalize() method has no effect on whether or not an object will be garbage collected (unless finalize() does something ugly, like storing a reference to the object somewhere.)
 
Mike Mc Afee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what Sun has to say about it Java 2 Platform SE 5.0 API
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic