I got it from
http://www.danchisholm.net/oct1/mybook/chapter16/exam1.html class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (
String[] args) {
new J().m1();
}}
Which object is not eligible for garbage collection after method m1 returns?
a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above
Answer is g.
Exlanation is : Please note that this question asks which object is NOT eligible for garbage collection after method m1 returns. The objects referenced by i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1 references any of those objects. When method J.m1 returns, the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects.
But i drawn the diagram i found non of the objects are eligible as objects other is pointing to other object like in statement i1.other(i3);
i1's other is pointing to i3 and not
i1 is pointing to i3 which holds true for every other statement. so all the referances will be there. so no object is eligible for GC.
Also please suggest me how to draw diagrams here to post .. Because just now i try to draw in
word but after that i couldn't able to cut and paste that diagram here.. so all my energy went waste.. so please help me out..
Thanks,
Geeta Vemula