Making a class a singleton can make it difficult to test clients.
package singletonProperty; //ingleton with public final field public class ElvisField { public static final ElvisField INSTANCE=new ElvisField(); private ElvisField(){ } }
package singletonProperty; //Singleton with static factory public class ElvisMethod { private static final ElvisMethod INSTANCE=new ElvisMethod(); private ElvisMethod(){ } public static ElvisMethod getInstance() { return INSTANCE; } }
package singletonProperty; //Enum singleton - the preferred approach public enum ElvisEnum { INSTANCE; }
A single-element enum type is the best way to implement a singleton