Hi JavaRanchers,
I'm having difficulty in the answer by K&B Book, for the question below. Given the code below.
On running the code, how many objects are eligible for Garbarge Collection when line 'do complex stuff' is reached.
class TestGC {
Short s = 200;
public static void main(
String args[]) {
TestGC t1 = new TestGC();
TestGC t2 = new TestGC();
TestGC t3 = t1.go(t2);
// do complex stuff
}
TestGC go(TestGC t) {
TestGC x = t
x = null;
return x;
}
} // end class
What is the correct answer
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
G. No possible to determine objects that are eligible for GC.
My answer was 'and is still' A. (0) because when this line 'do complex stuff' is reached the object references t1, t2, and the reference instance variables t1.s, t2.s are still referrering to objects on the Heap (which are 4 of them 2 TestGC objects and 2 Wrapper objects of type Short).
But the book says the correct answer is C. (2); two objects that is one referred to by t2 and the wrapper object (Short) referred to by reference instance variable s.
Can you please clarify me on the above? Thanks in advance.
Regards,
Siphiwe Madi
SCJP 5 (preparing)
[ May 13, 2008: Message edited by: Siphiwe Madi ]