饿汉式
class Singleton {
private static Singleton
instance=new Singleton();
private Singleton(){}
static Singleton getInstance() {
return instance;
}
}
懒汉式
class Singleton {
private static Singleton instance=null;
private Singleton(){}
static Singleton getInstance() {
if(instance==null)
instance=new Singleton();
return instance;
}
}