Hi,
i have a doubt in the below example:
public class Island {
Island i;
public static void main(
String [] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
my understanding is:
1.there will be 3 reference variables created in stack..i2,i3&i4
2.for each these 3 refences, there are 3 objects in heap.
3.Inturn these 3 objects will be creating 3 objects each for their instance reference variable "i"..so there is 6 objects in heap.
4.when we do i2.i = i3;only the reference variable "i" of i2 changed to i3 and correspondingly in all other assignments.
5.so as per my undertanding we will loose refence of instance variable (i) of each object. and the the reference pointing from stack is still pointing to the i2,i3,i4 onjects in heap.
6.thus i m confused,how this 3 objects are available for garbage collection?
7.only the instance object(i) of each i2,i3,i4 created should be available for garbage collection know..
please make understand this example.
regards,
prabhu