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

doubt in finalize()

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Taken from MasterExam:

I thought that finalize() can only be called once for an object, and since the program is already calling finalize(), gc will not call it.
Please explain.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
The finalize method is never invoked more than once by a Java virtual machine for any given object. In for loop you are yourself invoking the finalize method programmatically and not the JVM automatically ( which is done when objects are GCed).

Moreover, the finalize method is not guaranteed to be called by the GC. The only guarantee is that if called once by JVM it won't be called again by JVM at the time of garbage collection.
[ December 14, 2008: Message edited by: Harvinder Thakur ]

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks, Harvinder.
I thought that finalize() can be invoked only once for an object(program or JVM).
I agree. Here's the link:
subject: doubt in finalize()
It's not a secret anymore!