java StackOverflowError when local and instance objects creation

You call the constructor to create a new instance of your object. It has a reference to another instance, which you initialize with another new instance of ObjectType which, in turn, does the same thing. it’s an infinite number of calls until you get that error.

This will work.

public class ObjectTest { 

  public ObjectTest() { 

   } 


  public static void main(String[] args) { 

     ObjectTest localObj = new ObjectTest(); 
   } 
} 

Leave a Comment