Android: enum vs static final ints?

后端 未结 4 1432
闹比i
闹比i 2021-02-01 15:33

What are the advantages (or disadvantages) of having an enum versus having a set of static final ints in Java Android applications? Are there efficienc

4条回答
  •  清酒与你
    2021-02-01 16:24

    A very simple answer from personal experiences would be that Enums provide much better type safety or in other words the compiler gets to play a more active role in keeping your code bug free.

    On the other hand, because Enums are "second-class citizens" of the object world, they can be difficult to use in some of the more subtle design patterns commonly used today, especially when generics are involved.

    And finally, you can use static final ints in a bitfield. you couldnt do the following with an enum:

    int selectedOptions = Options.OPTION1 | Options.OPTION2 | Options.OPTION3;
    

提交回复
热议问题