Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 558 Bytes

File metadata and controls

26 lines (21 loc) · 558 Bytes

单例的最佳实现方式

public class Singleton {
	// Private constructor prevents instantiation from other classes
	private Singleton() { }

	/**
	* SingletonHolder is loaded on the first execution of Singleton.getInstance() 
	* or the first access to SingletonHolder.INSTANCE, not before.
	*/
	private static class SingletonHolder { 
			public static final Singleton INSTANCE = new Singleton();
	}

	public static Singleton getInstance() {
			return SingletonHolder.INSTANCE;
	}
}