Declaring an instance of a class inside that class

后端 未结 3 2106
不思量自难忘°
不思量自难忘° 2020-12-10 15:54

This code shows error at run time:

class Animal {
    Animal object1 = new Animal();

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


        
相关标签:
3条回答
  • 2020-12-10 15:58

    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.

    0 讨论(0)
  • 2020-12-10 15:59

    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.

    0 讨论(0)
  • 2020-12-10 16:17

    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.

    0 讨论(0)
提交回复
热议问题