Enumerations: Why? When?

后端 未结 7 1080
梦如初夏
梦如初夏 2020-11-30 08:37

I am an almost-graduating computer science student, and throughout my coding career, I\'ve found very few instances where enumerations, except for canonical cases such as re

相关标签:
7条回答
  • 2020-11-30 09:02

    What you would define as String / int constants before Java 1.5, you should now define as enums. For example, instead of having:

    public class StatusConstants {
       public int ACTIVE = 1;
       public int SUSPENDED = 2;
       public int TEMPORARY_INACTIVE = 3;
       public int NOT_CONFIRMED = 4;
    }
    

    You now have a safer and more developer-friendly:

    public enum Status {
       ACTIVE, SUSPENDED, TEMPORARY_INACTIVE, NOT_CONFIRMED
    }
    

    Same goes for anything that has more than one options, like statuses, street types (str., boul., rd., etc), user access levels (administrator, moderator, regular user), etc. etc.

    Check this for more examples and explanations.

    0 讨论(0)
提交回复
热议问题