//饿汉式
class Single //类一加载,对象就已经存在了。
{
private static Single s = new Single();
private Single(){}
public static Single getInstance(){
return s;
}
}
//懒汉式
class Single2 //类加载进来,没有对象,只有调用了getInstance方法时,才会创建对象
{ //延迟加载形式
private static Single2 s = null;
private Single(){}
public static Single getInstance(){
if(s = null){
s = new Single2();
}
}
}
//面试时好多时候考懒汉式
class Single //类一加载,对象就已经存在了。
{
private static Single s = new Single();
private Single(){}
public static Single getInstance(){
return s;
}
}
//懒汉式
class Single2 //类加载进来,没有对象,只有调用了getInstance方法时,才会创建对象
{ //延迟加载形式
private static Single2 s = null;
private Single(){}
public static Single getInstance(){
if(s = null){
s = new Single2();
}
}
}
//面试时好多时候考懒汉式