Enum Vs Constants

This has nothing to do with game programming, more just best java practices. Besides maybe readability, is there an advantage of using enums over finals/constants?

See: https://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel

Of this, this is my favorite answer: https://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel#answer-26118954

However, if you use an actual class as the type for your static final fields, then the only real advantages of enums over static final fields are:
4. Ability to use values in switch statement case statements without qualification (so without having to assign an ordinal manually to each value of a static final field and projecting that number for the switch)
5. Built-in sequentialization of values via ordinal()
6. Serialization by name not by value, which offers a degree of future-proofing (obviously depends on which serialization mechanism you actually use - I think they mean the standard Java serialization here)

When using classes as the types of static final fields, the following are no real advantages of enums anymore:

  1. Type safety and value safety
  2. Ability to define and override methods
  3. EnumSet and EnumMap (just use standard IdentityHashSet and IdentityHashMap)

Another advantage of enums is of course the ability to iterate over all enum literals, which you cannot do easily or efficiently with static final fields (i.e. Reflection or putting the field values in a custom collection).

Now, which way you choose just simply depends on what you want to do with the enum (or static final fields). For simple int/long values that require no further typedness and where you really only need the ordinal number, then static final int/long fields are good.

Ok cool that sums it up well! Thanks for the detailed answer :slight_smile: