title: Singleton pattern implementation from Wikipedia
body: <p>I am referring to the <a href="https://en.wikipedia.org/wiki/Singleton_pattern#Initialization-on-demand_holder_idiom" rel="nofollow noreferrer">solution for the Singleton Pattern by Bill Pugh</a> on Wikipedia:</p>
  
  <pre><code>public class Singleton
  {
  
      // Private constructor prevents instantiation from other classes
  
      private Singleton() {}
  
       /**
      * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
      * or the first access to SingletonHolder.INSTANCE, not before.
      */
  
      private static class SingletonHolder 
      { 
  
          private static final Singleton INSTANCE = new Singleton();
      }
  
     public static Singleton getInstance()
     {
  
         return SingletonHolder.INSTANCE;
     }
  
  }
  </code></pre>
  
  <p>Here they have mentioned:</p>
  
  <blockquote>
    <p>The inner class is referenced no earlier (and therefore loaded no earlier by the class loader) than the moment that <code>getInstance()</code> is called. Thus, this solution is thread-safe without requiring special language constructs (i.e. <code>volatile</code> or <code>synchronized</code>).</p>
  </blockquote>
  
  <p>However, isn't there a possibility that 2 threads would call <code>getInstance()</code> at the same time, which would lead to two instances of singleton being created? Isn't it safe to use <code>synchronized</code> here? If yes, where should it be used in the code?</p>