public class Sengleton{
private static Sengleton sengleton;
private Sengleton(){
System.out.println("just has a new sengleton object!");
}
public static synchronized Sengleton newInstance(){
if(sengleton==null){
sengleton=new Sengleton();
}
return sengleton;
}
public void display(){
System.out.println("test");
}
public static void main(String[] args){
Sengleton s=Sengleton.newInstance();
s.display();
Sengleton s1=Sengleton.newInstance();
s1.display();
System.out.println(s==s1);
}
}
输出结果:
just has a object!
test
test
true