I wondered what would happen with this example code:
public class Start {
public static void main(String[] args) {
new Start().go();
}
public Start() {
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
}
public boolean running = true;
public void go() {
while( running ) {
try {
Thread.sleep(10);
} catch ( Throwable t ) {}
}
}
}
public class A {
B b;
public void setB(B b) {
this.b = b;
}
}
public class B {
A a;
public void setA(A a) {
this.a = a;
}
}
It is obviously a stupid program, but: I wandered what would happen to the instances of A and B? They both are referred to by each other, so they shouldn't be considered collectable. But in fact, they ARE dead to the rest of the program, for they will never be referenced to again.
So my question is would they be garbage collected? Or are they dead memory?
Thanks in advance!