Last Updated:

Enumerations in Java : Advantages of using Enum

In this tutorial we will learn how to create an enumeration (Enum), what are the advantages and features of using enumerations. We'll also look at the use of Enum valueOf, Enum, EnumSet, and EnumMap values.

Enum is the keyword for creating the enumeration type. very much like a class. Let's look at an example of a simple enumeration.

A simple example of using Enum

In the example above, the ThreadStatesEnum class is Enum with a fixed set of START, RUNNING, WAITING, and DEAD constants.

Now let's see why enumeration (Enum) is better than regular field constants in Java classes.

For example, we create the same class with the same constants

And immediately look at the enumerations and class with constants in battle:

If we look at the example above, we immediately see two problems of using constants, which are solved with enam:

  1. We can pass any int to the parameter of the simpleConstantsExample(int i) method. This problem is solved only by a fixed value, which provides security.simpleEnumExample(ThreadStates th)
  2. We can change the value of a constant in a class, but the program will not throw any exceptions, although it may not work as expected. But if we change the Enum constant itself, we get an exception.intThreadStatesConstantClass

The result of the program will be as follows:

Advanced Enum features (enumerations)

Now let's look at the advanced features of enumerations (Enum) in the test class:

Enum Terms of Use

  1. All enumerations in Java implicitly extend a class that extends the class and implements interfaces, so that Enum cannot inherit
  2. You cannot end the package name with enum, for example, an invalid package
  3. Enumerations in Java can implement interfaces. (In the example above, there's Enum, which implements the .Closeable
  4. Enum constructors in Java are always private.
  5. You cannot instantiate an enumeration by using the .new
  6. We can create abstract methods in Enum, so all Enum fields must implement the abstract method. (In the example above, the method is abstract and all fields in Enum have implemented it.getDetail()
  7. We can define a method in Enum, and the fields can override them.
  8. All fields in Enum have a namespace, so we can only use a field with a class name: ThreadStates.START
  9. Enumerations can be used in . We'll see an example of use later in this tutorial.switch
  10. We can modify an existing enumeration without disrupting existing functionality. For example, we can add a new field in and this will not affect the existing functionality in any way.NEWThreadStatesClass
  11. A good tone is considered to be written in large letters and an underscore for a space. For example, etc.EAST, WEST, EAST_DIRECTION
  12. Enum constants implicitly static and final
  13. Enum constants are final, but you can change it. For example, we can use the method to change the priority of constants in Enum. (We'll see it in practice in the example below.)setPriority()
  14. We can safely compare constants using "" and the . They will both produce the same result.==equals()

Advanced features of Enum on the example

We already know the rules for using Enum, its advanced features, as well as the pitfalls. It's time to experience it all with an example:

The result of the program:

And now details on each function

  1. This method shows how to create an enumeration object and how we can use its methods.usingEnumMethods()
  2. The method shows the use of the java.util.Enum class method, with which we can create an enumeration object from a string.usingEnumValueOf()valueOf(enumType, name)
  3. The method shows the use of a method that returns an array that contains all the values of the enumeration in the order in which they were declared. Note that this method is automatically generated by the Java compiler for each enumeration. You will not find an implementation of the values() method in the java.util.Enum class.usingEnumValues()values()
  4. The method shows how to use Enum constants in a switch.usingEnumInSwitch()
  5. The method shows the use of java.util.EnumMap, which was introduced in the Java 1.5 Collections Framework. EnumMap is an implementation of Map. All keys in EnumMap must be of the same type, specified explicitly or implicitly, when the map is created. We cannot use EnumMap as a key.usingEnumMap()null

That's all you need to know about Enum in Java for comfortable and efficient work.