Why is enum the best implementation for a singleton?

后端 未结 3 1209
小鲜肉
小鲜肉 2020-12-13 15:36

I read Effective Java and there it\'s stated that a singleton is best implemented using enum.

This approach is functionally equivalent to

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 16:05

    Enums can't be inherited

    And it's one of the best parts of enums being singletons.

    If you can inherit from a singleton, it's not a singleton any more.

提交回复
热议问题