How does creating a instance of class inside of the class itself works?

前端 未结 5 670

What makes it possible to create a instance of class inside of the class itself?

public class My_Class
 {

      My_Class new_class= new My_Class();
 }
         


        
5条回答
  •  孤街浪徒
    2020-11-30 01:53

    Creating a instance of an object inside the object could lead into a StackOverflowError since every time that you create an instance from this "Test" class you will be creating another instance and another instance and so on.. try to avoid this practice!

    public class Test  {
    
        public Test() {  
            Test ob = new Test();       
        }
    
        public static void main(String[] args) {  
            Test alpha = new Test();  
        }  
    }
    

提交回复
热议问题