Sobes.tech
Junior — Senior

Mutual references between objects and garbage collector behavior in Java

livecode

Task condition

Consider how the system will behave when there are mutual references between objects. What will happen to the objects if, after their mutual linking, the references from local variables are nullified and garbage collection is requested?

class A {
    B b;
}

class B {
    A a;
}

public class main {
    public static void main(String [] args){
        A objA = new A();
        B objB = new B();

        objA.b = objB;
        objB.a = objA;

        objA = null;
        objB = null;

        System.gc();
    }
}