title: Creating singleton object, best way
body: <p>I want to create a singleton object and I found 3 ways, which one is better and why are others bad. The constructor is assumed to be private.</p>
  
  <p>Method 1:</p>
  
  <pre><code>class ClassX{  
     private static ClassX objX = null;
  
        static{
            objX = new ClassX();
        }
  
     //get objX method
   }
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code> class ClassX{  
     private static ClassX objX = new ClassX();
  
     //get objX method
   }
  </code></pre>
  
  <p>Method 3:</p>
  
  <pre><code> class ClassX{  
     private static ClassX objX = null;
  
     public ClassX getInstance(){
        if(objX == null)
            return new ClassX();
        else
            return objX;
     }
   }
  </code></pre>