java singleton instantiation

前端 未结 4 988
暖寄归人
暖寄归人 2020-12-24 04:01

I\'ve found three ways of instantiating a Singleton, but I have doubts as to whether any of them is the best there is. I\'m using them in a multi-threaded environment and pr

4条回答
  •  执笔经年
    2020-12-24 04:32

    Sample 1: Use if you don't need a lazy Singleton.
    Sample 2: Never use - it gets confusing with too much classes. And an inner class just to hold one variable seems to be a bit unnecessary. Sample 3: This is a lazy Singleton. Use it if you need it.

提交回复
热议问题