title: Thread-safe Enum Singleton
body: <p>Enums are good for creating singleton. I know enum methods are not thread-safe so I tried to make it thread-safe. Can anyone please confirm if this implementation is correct or not. Is it fine to use static and volatile so many places and can it be optimized? As inner class is private so I have to create functions in enum to access inner class functionality. Can it be optimized?</p>
  
  <pre><code>import java.util.Date;
  
  public enum SingletonWithEnum {
      INSTANCE;   
  
      private static class Singleton{
  
          private static volatile int count;
          private static volatile Date date;      
  
          public static  int getCount() { return count;}
  
          public static void setCount(int countParam) { synchronized(Singleton.class){ count = countParam; }}
  
          public static Date getDate() {  return date;}
  
          public static void setDate(Date dateParam) { synchronized(Singleton.class){ date = dateParam;}}
  
          public static String printObject() {
              return "Singleton [count=" + getCount() + ", date=" + getDate() + "]";
          }
  
      }
  
      public int getCount() { return Singleton.getCount();}
  
      public void setCount(int countParam)    {Singleton.setCount(countParam);}
  
      public Date getDate() { return Singleton.getDate();}
  
      public void setDate(Date dateParam) {Singleton.setDate(dateParam);}
  
      public String toString(){return Singleton.printObject();}
  };
  </code></pre>
  
  <p>I am using it like this.</p>
  
  <pre><code>SingletonWithEnum object1 = SingletonWithEnum.INSTANCE;
  object1.setCount(5);
  object1.setDate(new Date());
  </code></pre>