Actually, the full question (in my printing) is:
Which of these statements is true?
Select all valid answers.
(a) Objects can explicitly be destroyed using the keyword delete.
(b) An object will be garbage collected immediately after the last reference to the object is removed.
(c) If object obj1 is accessible from object obj2 and obj2 is accessible from obj2, then obj1 and obj2 are not eligible for garbage collection.
(d) Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed.
(e) If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is eligible for garbage collection.
Comments:
(a) false. There is no
delete in
Java. Garbage collection can never be forced, only suggested and it is through a different mechanism. (System.gc() or Runtime.getRuntime().gc()).
(b) false. There is no way to tell when an object will be garbage collected...the garbage collector
Thread is a low priority Thread (maybe it should unionize?)
(c) false. The key is that the active program/thread/method has to not have a reference to an object. Within any object of even minimal complexity there are tons of active references to other objects within in it.
Circular references don't matter...if they did then outer/inner class pairs couldn't be garbage collected (because an inner class has a reference to its enclosing class and if instantiated an outer class has a reference to its inner class). Another example of a circular reference is a Frame and its spawned Dialog.
(d) false. The object's finalize method which is called just before it is garbage collected could resurrect it.
(e) true. This is the simple case of an object with an instance member that is an object. Like,
public class Obj1 {
Obj2 obj2;
Obj1() {
obj2 = new Obj2();
}
// other methods
}
Now if somewhere in Obj1's methods, obj2 where set to null then the object it referred to would be eligible for garbage collection and an object obj1 still referred to elsewhere in the active program wouldn't be eligible for garbage collection but the statement stipulates that obj1
can reach obj2 so this isn't the case.
Instead, this is merely the case of an object, obj1, that is eligible for garbarge collection which has member variables, like obj2, that are
active object references looked at from the point of view of those objects referred to. They are eligible for garbage collection therefore it must follow that the object that refers to them must be eligible for garbage collection as well or the reference wouldn't be active ones.
Did I make this better or worse?
Best regards,
Steve Butcher
exceptionraised@aol.com [This message has been edited by sgwbutcher (edited June 28, 2000).]
[This message has been edited by sgwbutcher (edited June 28, 2000).]