The question here is, At which point is only one object eligible for garbage collection?
class Eco{
public static void main(String[] args){
Eco e1 = new Eco();
Eco e2 = new Eco();
Eco e3 = new Eco();
e3.e = e2;
e1.e = e3;
e2 = null;
e3 = null; //line 9
e2.e = e1; //here NullPointerException
e1 = null;
}
Eco e;
}
I am fine with the NullPointerException, but I also chose the answer "after line 9 runs". The answer in the
test makes it clear that the question creators intended to create a sort of island where all three objects are chained to each other.
However the way the code is presented, the island is not complete (e2 is not successfully chained to e1). If there is no chain to e1, however, I thought that at least e2 should be eligible for garbage collection as soon as e3 is nullified (and with it the last pointer to e2). Is this wrong thinking?
As I say, I know the code would not run to the end, but "eligible for gc" in my opinion meant "no matter whether the program stops at some point".