- 公有静态成员是final
public class Singleton {
public static final Singleton INSTANCE = new Singleton();
private Singleton(){}
public void say() {}
}
调用:
Singleton.INSTANCE.say();
- 公有的成员是个静态工厂方法
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton(){}
public static Singleton getInstance() {
return INSTANCE;
}
public void say() {}
}
调用:
Singleton.getInstance().say()
- 包含单元素的枚举
public Enum Singleton {
INSTANCE;
public void say() {}
}
调用:
Singleton.INSTANCE.say ()
枚举类的字节码就是一个final类型class,不能被继承,枚举值都是类静态常量