Why use Enums instead of Constants? Which is better in terms of software design and readability

JavaAndroidEnums

Java Problem Overview


I have a scenario in which I have Player types ARCHER,WARRIOR, and sorcerer.
What should I use in Player class for a player type?
Constant final static String variable or an Enum? and Why?
Please help with reasons.

Java Solutions


Solution 1 - Java

Suppose you use constant strings (or int values - the same goes for them):

// Constants for player types
public static final String ARCHER = "Archer";
public static final String WARRIOR = "Warrior";

// Constants for genders
public static final String MALE = "Male";
public static final String FEMALE = "Female";

then you end up not really knowing the type of your data - leading to potentially incorrect code:

String playerType = Constants.MALE;

If you use enums, that would end up as:

// Compile-time error - incompatible types!
PlayerType playerType = Gender.MALE;

Likewise, enums give a restricted set of values:

String playerType = "Fred"; // Hang on, that's not one we know about...

vs

PlayerType playerType = "Fred"; // Nope, that doesn't work. Bang!

Additionally, enums in Java can have more information associated with them, and can also have behaviour. Much better all round.

Solution 2 - Java

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic.

This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.

Additionally you can always use your enums as a String if you desire. Here is a reference.

Solution 3 - Java

Besides not letting you to provide an incorrect value, there is yet another feature of enums that may seem minor, but in my opinion is quite important. Modern IDEs can automatically suggest values for enums, while there is no way to reliably infer the possible values of a string constant (Intellij IDEA does the latter, but only for JDK classes and popular libraries). This is especially helpful when you are exploring a new API.

Solution 4 - Java

Enum is better to use for type safety. Wrong values cannot be entered. But enum in android takes so much memory, you should use intdef instead. Refer to this answer for Example and explanation:-

IntDef/StringDef Example

You can also check android source code it is replacing enums with IntDef/StringDef wherever possible. Ex. View.VISIBLE.

Solution 5 - Java

I would advice you to use enums only if you really need enumerated constants, or some additional functionality common for all items.

That's of course depending on the type of application you are writing and what versions and devices you want to support.

The reason is, enums add overhead because they allocate instances of their items. You can notice, that there are minimum enums in android platform, and almost all constants are final static ints (like View.GONE and stuff like that)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaqasView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaNitin ChhajerView Answer on Stackoverflow
Solution 3 - JavaNewlessClubieView Answer on Stackoverflow
Solution 4 - JavaHarish GyananiView Answer on Stackoverflow
Solution 5 - JavaAlex OrlovView Answer on Stackoverflow