目前最好的方式是用枚举来实现单例模式,如下:
public enum Elvis {
INSTANCE("wangfabo", 1);
private final int id;
private final String name;
Elvis(String n, int i) {
id=i;
name=n;
}
public void leaveTheBuilding() {
System.out.println("name:"+name+" id:"+id);;
}
public Elvis getInstance(){
return INSTANCE;
}
}