Java Enums can have behavior?

前端 未结 7 1399
清酒与你
清酒与你 2021-02-02 16:56

In Java, an Enum can do the great things that Enums do, but can also have methods (behavior and logic). What advantage does that have over using a class using an enum? Simple ex

7条回答
  •  孤独总比滥情好
    2021-02-02 17:27

    Enum types are also a great way to implement true singletons.

    Classic singleton patterns in Java typically involve private constructors and public static factory methods but are still vulnerable to instantiation via reflection or (de-)serialization. An enum type guards against that.

提交回复
热议问题