Let's say I have the following classes as described below:
class A {
private B b;
public A {
b = new B(this);
}
}
class B {
private C c;
private A a;
public B(A a) {
this.a = a;
c = new C(this);
}
public removeRefereceToC() {
c = null;
}
}
class C {
private B b;
public C(B b) {
this.b = b;
}
}
If we look at a graph of the references, A references B (and B references A), B references C (and C references B). Now, if we were to call B's method removeReferenceToC(), then B's reference to the object C would be eliminated, but C would still reference B. At this point would C be eligible for garbage collection?
The reason I'm not sure is that because of the direction of the references C can no longer be reached from B which makes me think that C is eligible for garbage collection, however the fact that C still references B is throwing me off.
So if B nulls out its reference to C, will C be eligible for garbage collection?
Edit: This question is being marked as a duplicate. I think the main difference between my question and the previously answered questions can be summed up by this: Previous questions: A -> B <=> C then the ref from A to B gets removed leaving A B <=> C
My question: A <=> B <=> C then the ref from B to C gets removed leaving A <=> B <- C