title: Implementing Singleton using factory method
body: <p>I am trying to create a class that has a singleton property using a static factory method..</p>
  
  <pre><code>package ishan.Beans;
  
  public class ControlManager {
  
      private static ControlManager controlManager=null;
  
      private  double id;
  
      private ControlManager()
      {
          this.id=Math.random();
      }
  
      public static ControlManager getControlManager()
      {
  
          if(null==controlManager)
              return new ControlManager();
  
          return controlManager;
      }
  
      public double getId() {
          return id;
      }
  
  }
  
  
  package ishan.Beans;
  
  public class Usage {
  
      public static void main(String a[])
      {
          ControlManager cManager=ControlManager.getControlManager();
  
          ControlManager c=ControlManager.getControlManager();
  
          System.out.println(c);
          System.out.println(cManager);
      }
  
  }
  </code></pre>
  
  <p>Every time I run this code, I get different instances of ControlManager in c and cManager. I am unable to figure out the problem or what is it that I am doing wrong. </p>