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

前端 未结 5 665

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:46

    The attribute to hold self instance should be static

    public class MyClass {
    
        private static MyClass instance;
    
        static {
            instance = new MyClass();
        }
    
        // some methods
    
    }
    

提交回复
热议问题