Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an instance of a class inside that class

Tags:

java

This code shows error at run time:

class Animal {
    Animal object1 = new Animal();

    public static void main(String[] args) {     
        Animal obj = new Animal();
    }
}

This is slightly different code with Animal obj = new Animal(); this line from main method commented out. this code shows no run time error.

class Animal {
    Animal object1 = new Animal();

    public static void main(String[] args) {     
        // Animal obj = new Animal();
    }
}

How is this caused and how can I solve it? I am using command prompt to run this code.

like image 299
Sachin Sabbarwal Avatar asked Nov 05 '11 21:11

Sachin Sabbarwal


3 Answers

If you have a member variable which is initialized to an instance of the same class, then when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class...

and then the stack will overflow and it will stop. It's OK for an object to have a pointer to another instance of the same class as a member, but it's not OK to create that instance in the constructor, or have it initialized in the body of the class, or you'lll recursively create objects until your stack overflows. Normally if you want such a member variable, then you accept the object as a constructor argument.

like image 178
Ernest Friedman-Hill Avatar answered Sep 20 '22 15:09

Ernest Friedman-Hill


Short answer infinite recursion.

Long answer if you want recursive data structures like that, you can do something like this:

public class A {
  A object1;
  public A(A member) { 
    this.object1 = member; 
  }
  public static void main(String[] args) {
    A item = new A(new A(null)); // note the base case/termination of the recursion!
  }
}

Or:

public class B {
  B object1;
  public void init() {
    object1 = new B();
  }
  public static void main(String[] args) {
    B item = new B();
    item.init(); 
    // now item.object1 != null, but item.object1.object1 == null
  }
}

In either case, you will have “sentinel” or “leaf” nodes in your data structure, which point to a null value.

like image 36
user268396 Avatar answered Sep 20 '22 15:09

user268396


It's a stack overflow.

It's similar to calling a function from the same function, like this:

void func(){
  func();
}

It will repeat until the stack fills, and then the program will crash.

Cheers.

like image 20
no_ripcord Avatar answered Sep 18 '22 15:09

no_ripcord