enum vs public static final

Technically one could indeed view enums as a class with a bunch of typed constants, and this is in fact how enum constants are implemented internally. Using an enum however gives you useful methods (Enum javadoc) that you would otherwise have to implement yourself, such as Enum.valueOf.

There is also .values() to iterate over the list of values

In summary:

  1. Type safety and value safety.
  2. Guaranteed singleton.
  3. Ability to define methods.
  4. Ability to use values in switch statement case statements without qualification.
  5. Built-in sequentialization of values via ordinal().
  6. Serialization by name not by value, which offers a degree of future-proofing.
  7. EnumSet and EnumMap classes.

Another post about getting enums by class name


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s