-1

My class:

public class Test {

    Test(){
        System.out.println("Hello World");
    }
    
    public void doSomething() {
        System.out.println("Do something");
    }

    Test t = new Test(); //why its is ok, although it gives run time stack overflow exception 
    
    public static void main(String[] args) {
        Test t = new Test();
        t.doSomething();
    }
}

its is giving exception, can anybody tell me why? because there is not compilation error when we create object outside main method, but when we running the program its gives error, why?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
paankajy
  • 29
  • 4
  • 1
    @GhostCat to be fair, the example given _is_ a minimal reproducible example. It is a self-contained class with a main method that you can run and reproduce the error (it's obvious what the error is without having to run it). What else did you want? – k314159 Mar 29 '21 at 09:17
  • 1
    Ask yourself: What happens when you call `new Test();`? – f1sh Mar 29 '21 at 09:18
  • @k314159 well Stackoverflow is all about documenting problems. So as much as possible we should keep following some guidelines. Even if the problem is easy. – Aris Mar 29 '21 at 09:18
  • @k314159 I honestly overlooked the "whole class thing" due to wrong formatting. But note that "some exception" still isnt a stack trace. I agree, the details are there, but a mcve that isnt recognizable as such ... – GhostCat Mar 29 '21 at 09:19
  • @Aristotle i am not sure what you are missing in this question. The code is there, there is a description of what happens. This is one of the better questions on SO. – f1sh Mar 29 '21 at 09:19
  • @f1sh It's an easy question, that's why it's easily understandable. Otherwise, you should provide your stack trace. Anyway, we are not here to argue if it's a good question or not, more to help, give advise and learn. – Aris Mar 29 '21 at 09:27
  • 1
    @paankayj Your `t` (the one outside your method bodies) is a *instance variable* (also called *instance member* or *instance field*). The expression behind the assignment operator (`=`) is an *initializer*, it gives `t` a value. This initializer is triggered prior to the execution of the constructor body, that is, it is effectlively the same as `Test() { this.t = new Test(); }`. And now it becomes much more obvious what will happen, as Osama already explained in his answer. – MC Emperor Mar 29 '21 at 09:41

2 Answers2

4

It's giving StackOverflowError because you're initializing an instance of a class inside its own initialization block. So when this class is initialized to be run, it runs its initialization block, which creates a new instance of itself and runs its initialization block again, and the process repeats again and again indefinitely. Once the call stack's limit exceeds by calling the initialization block again and again, the program emits StackOverflowError.

Osama A.Rehman
  • 912
  • 1
  • 6
  • 12
1

Its in an infinte loop when you create it inside itself

public class Test {

public Test(){
    System.out.println("Hello World");
}

public void doSomething() {
    System.out.println("Do something");
}

public static void main(String[] args) {
Test t = new Test();
t.doSomething();
}

}
SeanyMc
  • 425
  • 5
  • 10