public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
It can be argued that this example is effectively lazy initialization. Section 12.4.1 of the Java Language Specification states:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
Therefore, as long as there are no other static fields or static methods in the class, the Singleton
instance will not be initialized until the method getInstance()
is invoked the first time.