We'd need to know what was going on inside the constructor to answer the question fully.
But generally, as long as nothing retains a reference to the old object when the new one is created, it will be available for garbage collection, even if the old object is used in the process of creating the new one.
For example:
class Foo {
private String name;
Foo() {
this.name = null;
}
Foo(Foo f) {
this.name = f.name;
}
public void setName(String n) {
this.name = n;
}
public Foo spawn() {
return new Foo(this);
}
}
This won't retain references to the old Foo
when the new Foo
is constructed, so the old Foo
can be GC'd:
Foo f;
f = new Foo(); // The first Foo
f.setName("Bar");
while (/* some condition */) {
// Periodically create new ones
f = f.spawn();
}
Whereas if Foo
looked like this instead:
class Foo {
private Foo parent;
Foo() {
this.parent = null;
}
Foo(Foo f) {
this.parent = f;
}
public Foo spawn() {
return new Foo(this);
}
}
...then each newly spawned Foo
would have a reference to the previous Foo
and none of them would be available for GC.