class A{
A aob;
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A(); //tricky assignement
c=b; //one object eligible GC
c.aob=null;
System.gc();
}
}
There is two objectcs eligible for garbage collection but one is difficult to understand.
A d=new A().aob=new A();
1) This line I thing that it would make this
A d = new A().aob = new A();
^ ^
O1 O2
O1 --> O2 --> null
^
|
d ----|
2) But what really is doing is this (so one eligible object) WHY IS LIKE THIS?
A d = new A().aob = new A();
^ ^
O1 O2
O1 --> O2 --> null
^
|
d -----------|
because the assignements are associative right to left.
A d = ( new A().aob = new A() );
Could anyone explain it otherwise? Thanks