/**
* @author XQQ
* @create 2019-03-14 17:24
* @desc 懒汉式静态内部类
* 1、缺点可以用序列化可以破解
**/
public class LazySingleton {
public LazySingleton() {
if (sv.ls != null) {
throw new RuntimeException("已经实例化了");
}
}
public static LazySingleton getInit() {
return sv.ls;
}
private static final class sv {
static {
System.out.println(123);
}
public static final LazySingleton ls = new LazySingleton();
}
}
1、内部类在调用的时候才会实例化加载。
2、当反射实例化的是会抛异常。