How many objects are eligible for garbage collection when the main method of the Tester class reaches its end? My impression is that the answer is two, particularly a1, b1. However I have found somewhere as corect answer, that only a1 object is eligible. I think that since we have not asigned b1 as member variable in a2 an b1 is assigned to null before main ends, it should be collected by garbage collector. What is true?
class B { } class A { static B b1; B b2; } public class Tester { public static void main(String[] args) { B b1 = new B(); B b2 = new B(); A a1 = new A(); A a2 = new A(); a1.b1 = b1; a1.b2 = b1; a2.b2 = b2; a1 = null; b1 = null; b2 = null; } }