A code example:
public class Parent{
public void someMethod(){
//start of some sort of loop
Child child = new Child(this);
//do something
//replaces reference from old child to new child
child = new Child(this);
//repeats loop
}
}
public class Child{
Parent parent;
public Child(Parent parent){
this.parent = parent;
}
}
From what I understand of a object life cycle if a object were to be no longer referenced, then it would be eligible for gc, like when the child reference was changed to reference a new second Child object.
But what if each Child object has a reference to the object that created it, like above, would it be eligible for gc in this case once it is no longer referenced by its parent object?
If not, how can I write it so that it would be?