Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC 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 "GC" Watch "GC" New topic
Author

GC

Siva Sivaraman
Greenhorn

Joined: Sep 26, 2002
Posts: 17
The finalize() method is guaranteed to be called on any object to be GC�ed
True or False?
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Siva Sivaraman:
The finalize() method is guaranteed to be called on any object to be GC�ed
True or False?

True. From the JLS, section 12.6:
Before the storage for an object is reclaimed by the garbage collector, the Java virtual machine will invoke the finalizer of that object.
However, a particular object may never be garbage collected, so its finalize() may never be called.


SCJP 1.4
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by Siva Sivaraman:
The finalize() method is guaranteed to be called on any object to be GC�ed
True or False?

I'd say True -
this question is a little tricky ...
we know that actual garbage collection is never guaranteed when we make calls such as "Runtime.getRuntime.gc() or System.gc() however,
According to the book "Mike Meyer's Certification Passport Java 2" written by Glass,Griscti, Isayeva, Kallambella, Sierra
(pg.129) "Every object inherits a finalize() method from java.lang.Object class.
The garbage collector is garunteed to invoke this method once prior to reclaiming the object's memory"

And another book "Java 2 Sun Certified Programmer Study Guide" 2nd Edition from Osborne states:
"Every class has a special method called a finalizer that is called just before an object is collected. The JVM calls the finalizer for you as appropriate; you never call a finalizer directly."
Siva Sivaraman
Greenhorn

Joined: Sep 26, 2002
Posts: 17
Thanks to you all.
Didn't want to be trapped with such tricky questions.
The finalize() method is guaranteed to be called on any object eligible for GC.
True or False?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GC