title: Different ways to write singleton in Java
body: <p>The classic of writing a singleton in java is like this:</p>
  
  <pre><code>public class SingletonObject
  {
      private SingletonObject()
      {
      }
  
      public static SingletonObject getSingletonObject()
      {
        if (ref == null)
            // it's ok, we can call this constructor
            ref = new SingletonObject();
        return ref;
      }
  
      private static SingletonObject ref;
  }
  </code></pre>
  
  <p>and we can add synchronized keyword if we need it to run in multithreaded cases.</p>
  
  <p>But I prefer to write it as:</p>
  
  <pre><code>public class SingletonObject
  {
      private SingletonObject()
      {
          // no code req'd
      }
  
      public static SingletonObject getSingletonObject()
      {
        return ref;
      }
  
      private static SingletonObject ref = new SingletonObject();
  }
  </code></pre>
  
  <p>which I think is more concise, but strangely I didn't see any sample code written in this way, is there any bad effects if I wrote my code in this way?</p>