To create a truly single instance of your class (implying a singleton at the JVM level), you should make your class a Java enum
.
public enum MyClass {
INSTANCE;
// Methods go here
}
The singleton pattern uses static state and as a result usually results in havoc when unit testing.
This is explained in Item 3 of Joshua Bloch's Effective Java.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…