懒汉式
public class Single {
private static Single s = null;
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
private Single(){}
public static Single getInstance(){
if(s==null){
synchronized (Single.class){
if(s==null){
s = new Single();
}
return s;
}
}
return s;
}
}
public class Main{
public static void main(String[] args) {
Single s = Single.getInstance();
s.setStr("woshisssss");
Single a = Single.getInstance();
System.out.println(s.hashCode()+"-"+s.getStr());
System.out.println(a.hashCode()+"-"+a.getStr());
}
}