Sigleton bj
package com.bjsxt.base; class Sigleton{ private Sigleton(){}; private static Sigleton instance = new Sigleton(); public static Sigleton getInstance(){ return instance; } } public class TestSigleton { public static void main(String[] args) { Sigleton s1 = Sigleton.getInstance(); Sigleton s2 = Sigleton.getInstance(); System.out.print(s1==s2); } }
========================================================================
package com.bjsxt.base;
class Sig{
private static Sig instance = null;
private Sig(){};
public static synchronized Sig getInstance(){
if (instance ==null){
instance = new Sig();
}
return instance;
}
}
public class AAA {
public static void main(String[] args) {
Sig s1 = Sig.getInstance();
Sig s2 = Sig.getInstance();
System.out.print(s1==s2);
}
}