这是我我自己写的第一个单例模式的程序
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(null==instance){
instance=new Singleton();
}
}
}
下面是测试程序
public class TestSingleton{
public static void main(String args[]){
Singleton s1=Singleton.getInstance();
Singleton s2=Singleton.getInstance();
System.out.println(s1==s2);
}
}
输出结果为:true