File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Finalize Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finalize Method" Watch "Finalize Method" New topic

Finalize Method

Bubai Basu

Joined: May 23, 2007
Posts: 4
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

Joined: May 29, 2004
Posts: 75
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;

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.)

[Jess in Action][AskingGoodQuestions]
Mike Mc Afee

Joined: Jan 31, 2007
Posts: 19
Here is what Sun has to say about it Java 2 Platform SE 5.0 API
I agree. Here's the link:
subject: Finalize Method
It's not a secret anymore!