[Solved] creating an object outside main and user defined method scope gives “Exception in thread “main” java.lang.StackOverflowError” [closed]


Remove the line

Sample obj3 = new Sample();

because that will result in an endless loop (creating an instance of Sample while during the creation of an instance of Sample), causing your StackOverflowError.

And please dont post code as an image.

4

solved creating an object outside main and user defined method scope gives “Exception in thread “main” java.lang.StackOverflowError” [closed]