两种单例模式
public class Singleton {
private final static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
public class SingletonClass {
private static SingletonClass instance = null;
private SingletonClass() {
}
public static synchronized SingletonClass getInstance() {
if(instance==null) {
instance = new SingletonClass();
}
return instance;
}
}
Singleton instance1=Singleton.getInstance();
Singleton instance2=Singleton.getInstance();
System.out.println(instance1==instance2);