How many objects are eligible for garbage collector?


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;

The object initially assigned to the method scope variable b1 is not eligible for collection because the reference to it in Class A is static. It does not expire with that particular instance of a1. It is a sneaky bit of wonky java syntax that a1.b1 and A.b1 are the same reference, but it is what it is. That reference remains live until the class A is un-loaded/the JVM exists, regardless of what happens to any instance of A such as a1.

The code assigns the pointer b1 in the method to null, but it does not assign A.b1 to null.