3 Replies Latest reply: Mar 21, 2013 2:24 AM by Kayaman RSS

    Question on Singleton?

    909741
      hi,
      class A {
      private A a = new A();
      
        public static A getInstance(){
           return a;
        }
      
      }
      
      class A {
      
      private static volatile A _instance;
      
        public static A getInstance(){
      
        if(_instance == null){
        synchronized(A.class){
          if(_instance == null)
            _instance = new A();
          }
       }
      return _instance;
      }
      By the two ways we can achieve Singleton, can anyone please explain the first one having any disadvantage over the second one?