ENUMS IN JAVA
A Java enum is a data type that stores a list of constants. You can create an enum object using the enum keyword. Enum constants appear as a comma-separated list within a pair of curly brackets.
An enum, which is short for enumeration, is a data type that has a fixed set of possible values. A variable assigned from an enum can only have a value that appears in the enum. Enums help developers store data that they know will not change.
Declaring a Java Enum
Enums are declared using the “enum” type. Here’s the syntax of the “enum” keyword:
Let’s explain this syntax:
1) enum tells our program we want to declare an enumeration.
2) Name is the name of our enum
3) Value1, VALUE2, VALUE3, VALUE4 are the set of constant values our enum stores. These values are usually written in uppercase.
When to use Enums?
You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.
If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
This code returns:
Some important points on Java Enum :
- All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
- Enum has their own name-space. It means your enum will have a type for example “Direction” and you can not assign any value other than specified in Enum Constants.
- Enum can have fields, constructors, and methods.
- The enum constants have an initial value which starts from 0, 1, 2, 3, and so on. But, we can initialize the specific value to the enum constants by defining fields and constructors.
This code returns : Value of NORTH is 5
- Enum constants are implicitly static and final and can not be changed once created.
- You can not create instance of enums by using a new operator in Java because the constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
- Instance of Enum in Java is created when any Enum constants are first called or referenced in code.
- An enum specifies a list of constant values assigned to a type.
- An enum can be declared outside or inside a class, but NOT in a method.
- An enum declared outside a class must NOT be marked static, final , abstract, protected , or private.
- enum constructors can have arguments, and can be overloaded.
- enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.
- The semicolon at the end of an enum declaration is optional.
Java Enum Methods
Values():
The values() method returns a Java array which stores all the constants in an enum. Here’s an example of the values() method in action:
valueOf():
valueOf() accepts a string and returns the enum constant, which has the same name. So, if we wanted to retrieve the enum constant with the name NORTH, we could do so using this code:
This code returns:
name():
The name() method returns the name used to define a constant in an enum class. Here’s an example of the name() method being used to return the defined name of NORTH Direction.
This code returns:
toString():
toString() converts the name of an enum to a string. Here’s an example of toString() being used to convert the NORTH enum value to a string.
This code returns:
ordinal():
toString is important in enums. By using the ordinal() method, each enum constant index can be found, just like array index.
This code returns:
compareTo():
compareTo() compares the constants in an enum lexicographically and returns the difference between their ordinal values. Here’s an example of compareTo() being used with an enum value from our above example:
This code returns:
When to use name() and when to use toString()
If you need to get the exact name used to declare the enum constant, you should use name() as toString may have been overridden.
If you want to print the enum constant in a user-friendly way, you should use toString which may have been overridden.
Example of specifying initial value to the enum constants
This code returns:
Enums can be safely compared using:
- == operator or equals method
This code returns:
2. Switch-case statement
This code returns: