aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC (javaprepare.com) 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 (javaprepare.com)" Watch "GC (javaprepare.com)" New topic
Author

GC (javaprepare.com)

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

Which of the following statements related to Garbage Collection are correct. Select the two correct answers.
a. It is possible for a program to free memory at a given time.
b. Garbage Collection feature of Java ensures that the program never runs out of memory.
c. It is possible for a program to make an object available for Garbage Collection.
d. The finalize method of an object is invoked before garbage collection is performed on the object.

The answer given is c,d.
I have a question regarding d.
"The finalize method is never invoked more than once by a Java virtual machine for any given object." According to this statement option d is incomplete. Or may be I do not understand what the above statement means..
Any thoughts? Thanks.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Oblect class API:

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You are correct, Cathy. If finalize runs but the object is not gc'ed then finalize will not run later when the object is gc'ed. But "d" is correct too. Before an object is gc'ed, finalize will run. The object can not be gc'ed unless finalize has run. It may be that finalize ran long before the object was gc'ed but the question doesn't state "immediately" before garbage collection.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

It may be that finalize ran long before the object was gc'ed but the question doesn't state "immediately" before garbage collection.

I see your point Thomas.
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GC (javaprepare.com)