Declaring an instance of a class inside that class

后端 未结 3 2117
不思量自难忘°
不思量自难忘° 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 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.

提交回复
热议问题