Using Java Enums

In applications, you often need to work with a set of constant values. For example, representing a contract status with the “permanent”, “temp”, and “intern” values, or directions with the “north”, “south”, “east”, and “west” values.

In Java, you use the enum type (short for enumeration), a special datatype introduced in Java 5 to represent such list of predefined constants.

In this post I’ll discuss how to define and use enum types. I’ill also include example code along with JUnit test cases. If you are new to JUnit, I suggest going through my JUnit series of posts.

Defining Java Enums

You can define an enum type either independently outside of any class or as part of a class. The code to define an enum independently is this.

The preceding code defines an enum type, Days. The names of the enum type’s fields are in uppercase letters as they are constants.

Here is the JUnit test code.

The test code assigns a value to the day variable of the enum type, Days. The day variable can take any of the seven defined values. In this case it is set to SUNDAY. The test code then asserts the value.

The output on running the test in IntelliJ is this.

SImple Java Enum Test Output

You can also define an enum inside a class, like this.

SimpleEnumExample.java

Here is the JUnit test code.

The output on running the test in IntelliJ is this.

Java enum test output

Enums in if-else if Statements

Often you may require to compare a variable pointing to an enum constant against all the possible values in the enum. This can be done with if-else if statements.

The code to use enums in an if-else if statement is this.

EnumInIfStatement.java

Here is the JUnit test code.

The output on running the test in IntelliJ is this.
Test Output of Java Enum in If Statemnent

Enums in switch Statements

You can use enum in a switch statement. To do so, use the enum reference variable in the switch and enum constants or instances in case statements.

The code to use an enum type in a switch statement is this.

EnumInSwitchStatement.java

Here is the JUnit test code.

The output on running the test in IntelliJ is this.
Test Output of Java Enum in Switch Statement

Enum Iteration

You can iterate through an enum to access its values. The static values() method of the java.lang.Enum class that all enums inherit gives you an array of enum values.

Let us use the same enum type, Days defined in the SimpleEnumExample class and iterate through its values.

EnumIteration.java

The test code is this.

The output on running the test in IntelliJ is this.
Test Output Enum Iteration

Enum Fields and Methods

Java enums can have fields and methods. You can add fields against each enum constant and the enum gets initialized with the values. When defining the constants, the enum constructor must be supplied with the values.

Note: If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum.

The code to define an enum with fields, constructor, and methods is this.

EnumFields.java

The AbbreviationOfDays enum has an abbreviation field of type String. The constructor takes a String abbreviation and assigns it to the field. The getAbbreviation() method returns the abbreviation field.

Note: You can also define methods that do some logical operations within an enum.

Here is the Junit test code.

The output on running the test in IntelliJ is this.
Test Output Java Enum Fields Methods

Key Points

Some key points on enum type are:

  • Java enum extends java.lang.Enum class implicitly. Therefore, you cannot extend any other class in enum.
  • Enums can implement interfaces. They implicitly implement the Serializable and Comparable interfaces. This means if you need some common functionality along diverse enum types you can define it with an interface to have the enums provide the method implementations.
  • In Java, enum is a keyword.
    Enum constructors are always private or default. Therefore, you cannot have public or protected constructors in an enum type.
  • In an enum definition, comma separated constants must be terminated with semicolon.
  • You cannot create an instance of enum using the new operator.
  • You can declare abstract methods within an enum. If you do so all the enum fields must implement the abstract methods.
  • You can use “==” operator to compare enum constants effectively, since enum constants are final.

Summary

I often see code with lots of int constants representing a set of predefined values instead of enums, which is a better fit. I attribute this to inexperience programmers who are not familar enums.

Enums remove “magic numbers”. These are numbers that don’t really mean anything on their own. By replacing those numbers with actual values, your code makes more sense and follows better coding principles.

Also, enums prevent incorrect values from being passed to a function. Consider that you have a method that takes an int value. This method will execute if we pass any int value. But if you want to restrict the values that the method takes as input, you should ideally use enums. This is what is called type safety. Enums lets you control the required set of inputs.

In enterprise application development with Spring, you will find enums defined in model classes. This is for applications that expects the users to provide the exact enums in the request which adds a validation for the incoming request.

Share

Leave a Reply