Hi! I found this question in one of the mock exams. Which of the following statements about Java's garbage collection are true? a) The garbage collector can be invoked explicitly using a Runtime object. b) The finalize method is always called before an object is garbage collected. c) Any class that includes a finalize method should invoke its superclass' finalize method. d) Garbage collection behavior is very predictable. The answer is a,b,c. Please explain. Thank you Porchelvi
a. You may invoke the gc explicitely, though it is not guaranteed that it will run immediately. b. this behavior is mandatory (see JLS) c. in order to ensure proper finalization of objects the programmer should call the superclass's finalization method, to propagate finalization up the hierachy. This propagation is not done automatically by the compiler (like it is the case with constructors) Val [This message has been edited by Valentin Crettaz (edited September 23, 2001).]
I dont think "c" is the correct answer, as the word "should" is used, it is not mandatory/required to call finalize() of the super class. As far "a" is concerned , it is also poorly worded, we CANNOT invoke gc, all we can do is to TRY to invoke and the rest is dependent on JVM, and the bahaviour of JVM is very unpredictable. Please someone correct me if I am wrong. --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
a. the GC CAN be invoked though nothing is guaranteed, but it CAN be invoked !! c. the modal "should" is used here in order to emphasize that it's a good practice and clean implementation to do so !! I would be ok with your point if the verb was MUST, but should is ok !! When you write "should", you mean to give some piece of advice, and not to state some obligation... Anyway let's not argue about wording, which is truly poor by the way ! Val
a is correct because we can REQUEST to invoke garbage collection explicitly by invoking either Runtime.gc(). b is correct because finalize method will be called just before the object is collected. This is just a notification mechanism to tell that object is about to be collected. So, this will be called only before collection. c is correct, because finalize method will always be an override of a superclass method. So finalize method should invoke superclass's finalize method. This process is having a significance, i.e., before making an object available for garbage collection we should make sure that all it is not having any references, and d is not correct because garbage collection behavior will never be predictable. --SaiMurali. [This message has been edited by sai murali (edited October 03, 2001).]
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking