【发布时间】:2015-09-03 08:58:02
【问题描述】:
最好将单例实例声明为static 或static final?
请看下面的例子:
static 版本
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
static final版本
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return INSTANCE;
}
}
【问题讨论】:
-
阅读here 了解实施情况。
标签: java design-patterns static singleton final