0
public class Run{
 public static void main(String... args){
      A a1 = new A();
 }
}

class A{
  public A(){
    A a = new A();
  }
  //here as well A a = new A();
}

Why does this give a java.lang.StackOverflowError? Is there a recursive call happening here? How does it happen?

nbro
  • 15,395
  • 32
  • 113
  • 196
samsamara
  • 4,630
  • 7
  • 36
  • 66

7 Answers7

1

You're calling the constructor inside the constructor--that's what new does, constructs a new object.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • is there any guess that how many times it will call constructor. if i use that for recursion. – Raghavendra Sep 25 '14 at 13:22
  • 1
    @raghavendra Until you tell it to stop or the JVM blows up. – Dave Newton Sep 25 '14 at 13:31
  • thank you for the reply. but in eclipse i see that program is running only for 6 to 7 times – Raghavendra Sep 25 '14 at 13:48
  • Now i got it thank you this is my work out package tt.Exception; public class Exe { static int x; Exe() { try { new Exe(); } catch (java.lang.Error e) { System.out.println("Runtime Error"+x++); } } public static void main(String[] args) { new Exe(); } } – Raghavendra Sep 25 '14 at 13:54
1

Is there a recursive call happening here?

Yup

How it happens?

When you new A(), it calls the constructor for A, which does a new A() which calls the constructor, which does a new A() ... and so on. That is recursion.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

You can call, but it would be a recursive calling which will run infinitely. That's why you got the StackOverflowError.

The following will work perfectly:

public class Run{

 static int x = 1;
 public static void main(String... args){
      A a1 = new A();
 }
}

class A{
   public A(){
     if(x==1){
        A a = new A();
        x++;
    }
  }
}
nbro
  • 15,395
  • 32
  • 113
  • 196
Abhishek
  • 2,255
  • 1
  • 13
  • 21
0

The issue is that when you call the constructor, you create a new object (which means that you call the constructor again, so you create another object, so you call the constructor again...)

It is infinite recursion at its best, it is not related to it being a constructor (in fact you can create new objects from the constructor).

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

Basically none of your constructors will ever exit - each will get as far as trying to instantiate another object of type A recursively.

Rhys
  • 1,439
  • 1
  • 11
  • 23
0

You need to change your constructor to actually create an A object. Suppose A holds an integer value and nothing more. In this case, your constructor should look like this:

class A{
  int number;
  public A(){
      number = 0;
  }
}

What you're doing in your code is actually creating a new object instance inside of your own constructor.

So, when you call new A(), you're calling the constructor, which then calls new A() inside of its body. It ends up calling itself infinitely, which is why your stack is overflowing.

Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
0

I would think that there's a recursive call there. In order to create an A, you have to create another A inside of it. But to create that A inside of it, you have to create a third A inside of that A. And so on. If you use two different constructors or arguments or something though, you should be able to work around this:

class A {
    public A(boolean spawn){
        if (spawn) {
            A a = new A(false);
        }
    }
}
Panzercrisis
  • 4,590
  • 6
  • 46
  • 85