我在stackexchange中遇到了另一篇关于实现java单例的各种方法的文章.显示的方法之一是以下示例.它的投票率非常低.想知道为什么. 在Java中实现单例模式的有效方法是什么?
public class Singleton {
private static Singleton instance = null;
static {
instance = new Singleton();
// do some of your instantiation stuff here
}
private Singleton() {
if(instance!=null) {
throw new ErrorYouWant("Singleton double-instantiation, should never happen!");
}
}
public static getSingleton() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |