最好的单例模式
package test; public class Singleton { Singleton(){ System.out.println("**************"); } public static Singleton Instance(){ return Nested.instance; } public static void main(String[] args) { Singleton.Instance(); Singleton.Instance(); Singleton.Instance(); Singleton.Instance(); Singleton.Instance(); } } class Nested{ Nested(){ } static Singleton instance = new Singleton(); }
结果:只输出一行 ************