How to make sure that there is just one instance of class in JVM?

后端 未结 9 1658
离开以前
离开以前 2021-02-01 05:48

I am developing a design pattern, and I want to make sure that here is just one instance of a class in Java Virtual Machine, to funnel all requests for some resource through a s

9条回答
  •  暖寄归人
    2021-02-01 06:24

    class A{
        private A(){
    
        }
        public static A creator(A obj){
            A ob=new A();
            return ob;
        }
        void test(){
            System.out.println("The method is called");
        }
    }
    
    class Demo{
        public static void main(String[] args){
            A ob=null;
            ob=A.creator(ob);
            ob.test();
        }
    }
    

提交回复
热议问题