class ClassA {
ClassB mem1 = new ClassB();
ClassB mem2 = new ClassB();
}
class ClassB {
}
public class Sample {
public static void main(String[] args)
After obj1 is no longer referenced, it becomes eligible for garbage collection. mem1 still has a reference, so if java were to garbage-collect at this point, obj1 and mem2 it points too would be freed, but mem1 would remain untouched as obj2 still points to it.
Of course, after obj2 = null, it can also be collected.