title: Enums and Singletons
body: <p>Consider the following implementation</p>
  
  <pre><code>public enum Singleton {
  
      INSTANCE;
      private final OnlyOne onlyOne;
  
      Singleton() {
          onlyOne = new OnlyOne();
      }
  
      public static Singleton getInstance() {
          return INSTANCE;
      }
  
      public static void main(String[] args) {
  
          Singleton one = getInstance();
          one.onlyOne.method();
      }
  
  }
  
  class OnlyOne {
  
      public void method() {
          System.out.println("Hello World");
      }
  }
  </code></pre>
  
  <p>Here I have tried to implement the Singleton using <code>enum</code>. I want <code>OnlyOne</code> to have just one instance. My question is how do I restrict clients from instantiating objects of class <code>OnlyOne</code>? Because in some other class we can easily do this</p>
  
  <pre><code>OnlyOne  one = new OnlyOne();
  </code></pre>
  
  <p>I cannot provide a private constructor for it because doing so will break this</p>
  
  <pre><code>Singleton() {
    onlyOne = new OnlyOne();
  }
  </code></pre>
  
  <p>Do I need to use the enum as an inner member of <code>OnlyOne</code> class ? Any suggestions?</p>