102

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.

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
Waqas
  • 4,411
  • 6
  • 27
  • 31
  • 1
    I would not use a String variable. If anything, an `int` variable (with nicely named constants for the various types). – Thilo Jul 20 '12 at 08:13
  • 6
    is this answer any good to you? http://stackoverflow.com/questions/613837/enums-and-constants-which-to-use-when?rq=1 – Jimmy Jul 20 '12 at 08:13
  • possible duplicate of [What is Enum useful for?](http://stackoverflow.com/questions/11393389/what-is-enum-useful-for) – oers Jul 20 '12 at 08:27
  • 1
    Possible duplicate of [Enums and Constants. Which to use when?](https://stackoverflow.com/questions/613837/enums-and-constants-which-to-use-when) – RBz Nov 19 '18 at 11:28
  • @RBz That question is for C#, and C# enums are very different from Java enums (basically just constants vs full-fledged objects). So, not a duplicate – Mark Rotteveel Nov 20 '18 at 12:29
  • @MarkRotteveel That question had a language agnostic tag! – RBz Nov 21 '18 at 14:31
  • 2
    @RBz That doesn't change the fact the enums in Java are a very different beasts from enums in C# and other C-like languages. And even though it is tagged language-agnostic, the answers on that question are all C#-specific. – Mark Rotteveel Nov 21 '18 at 14:47

5 Answers5

156

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    What about performance comparison between Enums and Constants? – Waqas Jul 20 '12 at 08:20
  • 3
    @waqas716: Enums are reference types, but there's only one object per value, so you're unlikely to waste much memory. If you want other performance differences, you'd have to say what you're interested in. – Jon Skeet Jul 20 '12 at 08:27
  • 1
    I have to send this Player object to a webService, So I will convert this object to Json String, In this case what should I use String Constants or Enum? – Waqas Jul 20 '12 at 10:10
  • You can always use a map to go from Enums to Strings. (There's also a Bi-Directional map in Apache's Commons Collections 4 that allows you to go from Strings to Enums if that's required too: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html) – Jacob Holloway Oct 07 '16 at 18:16
19

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.

Community
  • 1
  • 1
Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35
8

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.

NewlessClubie
  • 983
  • 2
  • 8
  • 18
2

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.

Community
  • 1
  • 1
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
-1

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)

Alex Orlov
  • 18,077
  • 7
  • 55
  • 44