1. Singleton
public enum Singleton {
YANLIAO;
private String name;
private Singleton(){
System.out.println("初始化");
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
2. Client
public class Client {
public static void main(String[] args) {
Singleton.YANLIAO.setName("BBB");
System.out.println(Singleton.YANLIAO.getName());
System.out.println(Singleton.YANLIAO.getName());
System.out.println(Singleton.YANLIAO.getName());
System.out.println(Singleton.YANLIAO.getName());
System.out.println(Singleton.YANLIAO.getName());
}
}
输出结果
初始化
BBB
BBB
BBB
BBB
BBB