How to make an immutable singleton in Java?

后端 未结 4 1833
后悔当初
后悔当初 2021-01-19 01:26

An immutable object is initialized by its constuctor only, while a singleton is instantiated by a static method. How to make an immutable singleton in Java?

4条回答
  •  误落风尘
    2021-01-19 02:04

    public enum MySingleton {
    instance;
    //methods
    }
    
    //usage
    MySingleton.instance.someMethod();
    

提交回复
热议问题