50,530
社区成员
发帖
与我相关
我的任务
分享
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {
System.out.println("constructor");
}
/**
* SingletonHolder is loaded on the first execution of
* Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
* not before.
*/
private static class SingletonHolder {
public static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
public static void main(String[] args) {
System.out.println("main");
getInstance();
}
}