使用枚举实现单例
public class SingletonTest {
private SingletonTest() {
}
public static SingletonTest getInstance() {
return Singleton.INSTANCE.getInstance();
}
private enum Singleton {
INSTANCE;
private SingletonTest singleton;
// JVM保证这个方法绝对只调用一次
Singleton() {
singleton = new SingletonTest();
}
public SingletonTest getInstance() {
return singleton;
}
}
}
I want to know why

浙公网安备 33010602011771号