We need to know what is going on inside the constructor in order to fully answer the question.
But in the general case, while nothing retains a link to the old object when creating a new one, it will be available for garbage collection, even if the old object is used in the process of creating a new one.
For instance:
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 will not save references to the old Foo when building the new Foo , so the old Foo may be GC'd:
Foo f; f = new Foo(); // The first Foo f.setName("Bar"); while (/* some condition */) { // Periodically create new ones f = f.spawn(); }
If Foo looked like this:
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 created Foo will refer to the previous Foo , and none of them will be available to GC.
source share