Singleton pattern can be one of the most common design patterns, there are hungry man pattern, lazy man pattern. The following code is probably the best lazy mode, loaded in time and thread-safe.

public class Singleton {

    private Singleton() {} /** * gets the singleton ** @return
     */
    public static Singleton getInstance() {
        returnSingletonHolder.intstance; } /** * a private static inner class, Private static class SingletonHolder {private static final Singleton intstance = new Singleton(); }}Copy the code

Static inner classes are loaded only when used.