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.
I have a scenario in which I have Player types ARCHER , WARRIOR ,
Share
Suppose you use constant strings (or
intvalues – the same goes for them):then you end up not really knowing the type of your data – leading to potentially incorrect code:
If you use enums, that would end up as:
Likewise, enums give a restricted set of values:
vs
Additionally, enums in Java can have more information associated with them, and can also have behaviour. Much better all round.