枚举方式创建单例对象

public class TestSingleton {

    private TestSingleton(){}

    public static TestSingleton getInstance(){
        return SingletonEnum.INSTANCE.getTest();
    }

    private enum SingletonEnum{
        INSTANCE;

        private TestSingleton singleton = null;
        SingletonEnum(){
            singleton = new TestSingleton();
        }

        private TestSingleton getTest(){
            return singleton;
        }

    }
}
原文地址:https://www.cnblogs.com/qzg3362/p/9608503.html