After reading some questions and answers about enum I don't find it really useful...
It's something between class and a variable but I know where can I use it so it would be more useful then a class, or a couple of variables for example.
After reading some questions and answers about enum I don't find it really useful...
It's something between class and a variable but I know where can I use it so it would be more useful then a class, or a couple of variables for example.
There are other things you can do and reasons to use them, but the primary reason I use enums is to prevent the possibility of an invalid parameter. For example, imagine the following method:
public void doSomethingWithAMonth(int monthNum);
This is not only ambiguous (do month indexes start at 1, or at 0?), but you can give down-right invalid data (13+, negative numbers). If you have an enum Month
with JAN, FEB, etc. the signature becomes:
public void doSomethignWithAMonth(Month month);
Code calling this method will be far more readable, and can't provide invalid data.
I use Enums for anything that has multiple nonchanging "type" of something. i.e days of a week.
Why I don't use String in this case is because I can't do switch statement on the String and I can on Enum so for every day of a week I can do something specific.
I also use Enum when working with singleton pattern, Check the Effective Java book CH2(CHAPTER 2 CREATING AND DESTROYING OBJECTS), quote :
"While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton." not gonna paste code read the book it's excellent.
If I were you I'd firstly read Thinking in java chapter Enumerated Types. Then after that Effective Java chapter 6 and you should be good
Simplest Definition - An enum is just like any other class, with a predefined set of instances.
Best part of enum is it can be used in switch statements where we can't use Strings.
and Enum is a safe-type so you can't assign a new value at the runtime.
An enum
forces coders to select from a pre-defined list of entities or concepts. Whilst you could achieve the same objective with classes or setting a variable to a particular known value, an enumeration is less error-prone as it advertises and enforces a menu from which the setter of a value can pick.
An Enum is useful to prevent people from doing things they shouldn't do. For example, if I have:
public final static int WEATHER_SUNNY = 0;
public final static int WEATHER_RAINY = 1;
public final static int WEATHER_HAIL = 2;
then I can do: MyClass.WEATHER_SUNNY + MyClass.WEATHER_HAIL
which makes no sense. That's why it's better to have a Weather enum. This makes the code more readable and it disallows summing two weather integers.
I would add to the other answers that enums are classes which only have a fixed number of instances. These instances may have fields and methods, and be used as any other Java objects. For example, the Month
enum could define methods like
public int getNumberOfDays(int year);
public boolean isFirstOfYear();
As goes from the name "enum" it is ideal for enumeration constants, which provide extended functionality. As for example you can store some constants comprising from several parameters like: constantId, constanNameString, additional params. Additionally enum can contain functions.
An exotic use of enum is to make a singleton.
Enum is useful when you have a list of values that are functionnally significant and finite.
Say for exemple that you need to use a day of the week : another day won't be added and you'd rather have Day.MONDAY in your code rather than a number and having to remember that 0 is monday, 1 is tuesday and so on. It makes switch statement easier to understand too, with :
Switch(day) {
case Monday : ... break;
case Tuesday : ... break
etc.
}
Among other things, an Enum is useful to create singletons:
What is an efficient way to implement a singleton pattern in Java?