-1

Why I am getting StackOverflowError at line 11. The line that I am getting error: System.out.println(new JavaInnerClass().callInnerClassMethod(animalQuantity));

Here is the full code:

public class Dog extends Animal {
private String animalName;
private int animalQuantity;

public Dog(String animalName, int animalQuantity) {
    animalName(animalName);
    quantity(animalQuantity);
    // JavaInnerClass.tempDog name = new JavaInnerClass.tempDog();
    // System.out.println(name.totalQuantity(animalQuantity));
    System.out.println(new JavaInnerClass().callInnerClassMethod(animalQuantity));
}

@Override
public String animalName(String animalName) {
    this.animalName = animalName;
    return animalName;
}

@Override
public int quantity(int animalQuantity) {
    this.animalQuantity = animalQuantity;
    return animalQuantity;
}

}

the JavaInnerClass:

public class JavaInnerClass {

Dog[] dog = { new Dog("Husky", 90), new Dog("Boxer", 100) };

tempDog temp = new tempDog();

public static class tempDog {

    public int totalQuantity(int quantity) {
        return quantity + 200; // assuming a statement
    }

}

public int callInnerClassMethod(int quantity) {
    return temp.totalQuantity(quantity);
}

public static void main(String[] args) {
    new JavaInnerClass();
}

}

Chinmoy Acharjee
  • 520
  • 5
  • 16
  • Post the stack trace, or at least the first several lines until it starts repeating. That will show you the exact sequence of recursive calls that are the problem. – chrylis -cautiouslyoptimistic- Jun 26 '17 at 20:45
  • 1
    `JavaInnerClass` constructs `Dog` instances on initialization, which in turn constructs a `JavaInnerClass` instance in its constructor. Hence the stack overflow. – shmosel Jun 26 '17 at 20:45
  • 1
    @chrylis It's entirely possible that the OP doesn't know what a stack trace _is_. It's helpful to have this information, but with obviously new users to Java, you might want to explain what you mean by posting a stack trace. – jwir3 Jun 26 '17 at 20:48
  • @jwir3 The first page of Google results for "stack trace" contains several helpful explanations. – chrylis -cautiouslyoptimistic- Jun 26 '17 at 20:56
  • 1
    @chrylis Ah, we're expecting users to _search_ for things now? Sheesh how elitist has SO become? :D – jwir3 Jun 26 '17 at 20:57

3 Answers3

1

The JavaInnerClass constructor invokes the Dog constructor that invokes the JavaInnerClass constructor that invokes the Dog constructor ... and so on...


Look at the JavaInnerClass constructor.

It initializes the array field dog :

Dog[] dog = { new Dog("Husky", 90), new Dog("Boxer", 100) };

But what does the Dog constructor ?
It creates an instance of JavaInnerClass :

public Dog(String animalName, int animalQuantity) {
     ...
    System.out.println(new JavaInnerClass().callInnerClassMethod(animalQuantity));
}

And all that goes on until the stackoverflow.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

You are creating dogs in JavaInnerclass

Dog[] dog = { new Dog("Husky", 90), new Dog("Boxer", 100) };

They are created everytime a new object is instantiated.

And when you create a new Dog, you create a new JavaInnerClass

new JavaInnerClass().callInnerClassMethod(animalQuantity)

It's a cycle.

ElDuderino
  • 3,253
  • 2
  • 21
  • 38
0

The reason this is happening is that when Dog is constructed, the line you mentioned creates a new JavaInnerClass object:

public Dog(String animalName, int animalQuantity) {
    animalName(animalName);
    quantity(animalQuantity);
    // JavaInnerClass.tempDog name = new JavaInnerClass.tempDog();
    // System.out.println(name.totalQuantity(animalQuantity));
    System.out.println(new JavaInnerClass().callInnerClassMethod(animalQuantity));
}

But, then then JavaInnerClass constructor creates a new Dog:

public class JavaInnerClass {
// this program is written to understand inner class..
Dog[] dog = { new Dog("Husky", 90), new Dog("Sheppered", 100) };
...
}

Which, in turn, creates a new JavaInnerClass, and so on... until the system runs out of stack space.

So, you need to make sure you don't have a cycle of method calls when calling recursive methods. It also seems as though you have a few issues with a class defined inline, so perhaps if you told us what you're trying to do, we might be able to help you out a little more.

jwir3
  • 6,019
  • 5
  • 47
  • 92