compile time error – stackOverflow, while creating new class instance [closed]

When you do new Test(), the instance fields of Test will be initialised. In this case, tt will be initialised.

Well, how do you initialise tt? You call its constructor. When you do so, tt.tt will need to be initialised. This will call the constructor again, which tries to initialise tt.tt.tt. And it goes on and on like this.

So the Test constructor is calling the same Test constructor, causing a stack overflow. The situation is kind of like this:

Test tt;

public Test() {
    tt = new Test();
}

Leave a Comment