Declaring an instance of a class inside this class

This code shows a runtime error:

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

This is a slightly different code with Animal obj = new Animal(); commented on this line from the main method. this code does not show runtime errors.

 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 use the command line to run this code.

+6
source share
3 answers

If you have a member variable that is initialized by an instance of the same class, then when this instance is created, it will also have a member variable that is initialized by an instance of the same class, and when that instance it will also have a member variable. which is initialized by an instance of the same class, and when this instance is created, it will also have a member variable that is initialized by an instance of the same class, and when this instance it will also have a member variable, which is initialized an instance of the same class, and when this instance is created, it will also have a member variable that is initialized by an instance of the same class, and when this instance it will also have a member variable that is initialized by an instance of the same class, and when this an instance will be created, it will also have a member variable that is initialized by an instance of the same class, and when this instance is CRE ated, it will also have a member variable that is initialized by an instance of the same class, and when this instance If it is created, it will also have a member variable that is initialized by an instance of the same class, and when this instance it will also have a member variable that is initialized by an instance of the same class, and when this instance is created, it will also have a variable- a member that is initialized by an instance of the same class, and when this instance it will also have a member variable that is initialized by an instance of the same class, and when this instance is created, it will also have a member variable that and is initialized by an instance of the same class, and when this instance it will also have a member variable that is initialized by an instance of the same class, and when this instance is created, it will also have a member variable that is initialized by an instance of the same class ...

and then the stack overflows and it stops. This is normal if the object has a pointer to another instance of the same class as the member, but not so that you can create this instance in the constructor or initialize it in the class body, or you will recursively create objects until your stack overflows. Usually, if you want such a member variable, you take an object as an argument to the constructor.

+13
source

The short answer is infinite recursion.

Long answer, if you need recursive data structures, 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 any case, you will have sentinel or leaf nodes in your data structure that point to null .

+5
source

This is a stack overflow.

It is similar to calling a function from the same function, for example:

 void func(){ func(); } 

It will be repeated until the stack is full, and then the program crashes.

Greetings.

+2
source

Source: https://habr.com/ru/post/900856/


All Articles