Please provide the answers for the following question.
37. 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.
My knowledge says (b) is the only b is correct. But the key says a,b,c are all correct.
a)This is wrong because the System.gc() and Runtime.gc() just allow us to request the gc for collection but they wont guarantee any.
c) c is wrong because the finalize() method in Object doesnt have any specific implementation. So there is no reason in calling super.gc()
a) Consider this from the System.gc() API documentation:
When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
That is saying that the garbage collector will run (but it does not specify that any particular objects will be collected).
c) What about a class C that extends class B that extends class A that extends class Object? Classes B and A may each have a finalize() method that must be called during the finalization of an instance of class C. To make sure that the finalize method is run you should use super.finalize() in the finalize methods. Note: you do not call super.gc() as you have written above. [ January 31, 2007: Message edited by: Barry Gaunt ]
My apologies about option c, I want to mention super.finalize().
But can you please confirm "is it mandatory to call super.finalize() or is it a good practice to call super.finalize(), while overriding finalize() method". Kindly suggest the answers for the above question also.
P.S.: My intention is not to let any body think. However i follow your advice.