单例模式 饿汉式
静态变量
class Singleton { private static final Singleton instance = new Singleton(); private Singleton() { } public static Singleton getInstance() { return instance; } }
静态代码块
class Singleton { private Singleton() { } private static final Singleton instance; static { instance = new Singleton(); } public static Singleton getInstance() { return instance; } }