I am not sure if I have understood the behaviour of garbage collector completely, therefore I am posing this question based on a previous question.
In this program:
class A {
Boolean b;
A easyMethod(A a){
return new A();
}
public static void main(String [] args){
A a1 = new A();
A a2 = new A();
A a3 = new A();
a3 = a1.easyMethod(a2);
a1 = null;
// Some other code
}
}
how many objects would be eligible for garbage collection? I think that although a3
never becomes null, the first object (new A()
) assigned to it should also be garbage-collected, as no references still point to it. Am I right? I think hence that the correct answer would be again 2 objects. What is true actually?