Tuesday, October 2, 2012

Singleton object using Enum type

Since Java 1.5, there is a new approach to implement Singletons. Simply make it an enum type :
public enum MySingleton {

  INSTANCE;

  //Singleton method
  public void someMethod( ) {...}
}
Accessing the enum singleton :
MySingleton.INSTANCE.someMethod( );

No comments: