to explain you I have taken the full code from there:
at line 1, 3 objects are created
firste one refered by gb1, second refered by gb1.g, and third refered by gb1.g.g variables.
And at line 2, 2 objects are created first one refered by gb1.g.g and the second one refered by gb1.g.g.g variables.
when i am drawing a graph i get 2 objects eligible for Gc..how do you get answer as 3?in this program??
Garb gb1 = new Garb(new Garb(new Garb(null)));
gb1 is referring to one garb object,then another instance gb1.g is referring to another garb object ,then third instance gb1.g.g is referring to third garb object which is a null object....M i right???
In second line
gb1.g.g=new Garb(new Garb(null));
we are referring gb1.g.g to a new object ...so the object it was referring first has no live reference referring it right???so it becomes eligible for Gc.....
now gb1.g.g refers to a new garb object and further gb1.g.g refers to another garb object which is a null object right???
In third line
Garb gb2=new Garb(new Garb(new Garb(gb1)));
gb2 is pointing to new garb object,gb2.g is pointing to another garb object and gb2.g.g is referring to garb object which is having reference variable gb1???now here by keeping gb1 in objects value's place what does it mean???
According to fourth line i can conclude that the both gb1.g and gb2.g will refer to same object created by gb1.g.....then the object pointed by g2,g no longer has a live reference so it is eligible for gc....
i m confused with 3 line....and if gb2.g is leaving it pointer and referring to a new pointer what happens to gb2.g.g referenced object???i hope you have got my state of confusion.......
in this program, on line 1, 3 objects are created. Garb1,2,3.
Each Garb is having one reference variable, g. When Garb is created, g in that particular Garb is assigned a reference.
Now, on line1 starting from rightmost side, first Garb3 (innermost) is created, its "g" is assigned null.
next, Garb2 is created and at the time of creation, Garb3 is passed to its one-arg constructor, "g" in Garb2 is assigned to Garb3.
similar for Garb 2 and 1.
then Garb1 object is assigned to gb1. so to access Garb3 using gb1 we use, gb1.g.g. Means using "g" in Garb2. likewise for others.
now on line 2:
this sets initial Garb2.g reference to Garb4
so it is like.
Garb3 eligible for GC.
now for gb2:
gb2.g is "g" in Garb6 used to access Garb7 and we know gb1.g is "g" in Garb1 used to access Garb2.