Hello all,
This will be a little how to on the use of enums. I've seen a few questions pop up regarding these so I thought I'd write a little how to revealing some common methods etc for enums.
It's important to remember that all the values that you declare in an enum are all instances of that enum. Complicated?
If I create an enum called Day that holds the days of the week MONDAY to SUNDAY, calling Day.MONDAY actually returns an instance of the enum itself with the name of MONDAY.
Methods
These methods are all available on the enum without overriding or implementing them, these get explicitly inherited.
String name()
Returns the name of this enum constant, exactly as declared in its enum declaration.
int ordinal()
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
String toString()
Returns the name of this enum constant, as contained in the declaration.
<T> valueOf(String)
Returns the enum who's name matches the input string.
<T[]> values()
Returns an array of all the enums values.
Adding methods to an enum
You can also add your own methods and constructors to an enum which I will show you how to do further down. This is useful if you for example would like to have an enum like the Day example which also gives you more information, such as for instance a boolean saying if this day is a weekend day or not.
Example 1
This creates the most simple form of enums, it only holds the values we define, with no extra methods.
public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; }
Example 2
This is the above enum created with some extra information. It holds a boolean telling us if the day is during a weekend or not. You can see that I've actually created two constructors here, one which takes no arguments and sets the weekend as default to false and another one which takes a boolean and sets the weekend value to whatever is passed in.
When I then declare these enums I use the first constructor on MONDAY to FRIDAY as you can see below and on SATURDAY and SUNDAY I pass in a boolean of true to say that these two days are weekend days.
public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY(true), SUNDAY(true); private boolean weekend; private Day() { this.weekend = false; } private Day(final boolean weekend) { this.weekend = weekend; } public boolean isWeekend() { return weekend; } }
This will let you check if the enum is a weekend day or not by calling the method isWeekend().
final Day day = Day.SATURDAY; System.out.println(day.toString() + " is a weekend day is " + day.isWeekend());
Conclusion
This was a short how to on how to create enums, we've dealt with a most simple form of enums as well as adding extra methods, members and constructors.
If there are still some question marks regarding enums please let us know and I'm sure either me or anyone else with some enum knowledge on these forums will answer your question.
Enjoy! Happy coding!
// Json