三种单例模式:
public class Singleton2 {
private static Singleton2 s;
private Singleton2() {}
static {
s = new Singleton2();
}
public static Singleton2 getInstance() {
return s;
}
}
public class Singleton {
private static Singleton s;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if(s == null)
s = new Singleton();
return s;
}
}
public class Singleton3 {
private static class InSingle{
private static final Singleton3 s = new Singleton3();
}
private Singleton3() {}
public static final Singleton3 getInstance() {
return InSingle.s;
}
}