Enums in Java – Exploring the basics

In the world of Java, we often come across a situation when we want our method to have certain values as argument, in other words we want to restrict the values to set of predefined values. Well, Enums are there for you in such situations.

Enum is nothing but a special type of Class. We will come across the justification soon.

So, here is a formal definition: Java let’s us to restrict a variable to hold only a particular value from a list of predefined values, what we call them, is the Enumerated List. The items that we specify in the enumerated list are surprisingly called Enums.

Let’s explore Enum with a very basic example. Assume we are creating a Traffic Light Application, where we are going to accept a color of the light as an input. So basically, we want to restrict the input to only one of the three colors, namely: Red, Green, and Yellow. However, if we opt to keep the argument type as String, user will be able to send any value and there will not be any desired restriction in the input value. Let’s suppose the user sends a string ‘Black’ as Color, then we might expect the application to malfunction. We can now infer that String as the input datatype is going to be a bad choice here.

So let us see how we can achieve it through Enum.

Note that we have restricted the argument to be of Enum type TrafficLightColor. Therefore we can say Enum provides type safety.  

Therefore, the only possible calls to the above method changeLight are:

This way, we can make our code less error prone and bug free.

Enums can be declared in the following mentioned possible ways:

1. In an independent java file (TrafficLightColor.java)

2. Inside a class (TestOuter.java)

Note that Enums cannot be declared within a method.

Now, let us see what actually happens behind the scene when we create an Enum.

The most important thing to remember is that Enums are not String or ints! Each of the enumerated TrafficLightColor types is actually an instance of TrafficLightColor. In other words RED is of the type TrafficLightColor. Let’s frame the same Enum TrafficLightColor as being a class. It would look something like:

Use the above snippet only for understanding purpose, as the actual internal implementation of Enums might be different from what we have mentioned.

The above snippet however has helped us inferring few more things about Enums:

  1. Each of the enumerated values RED, GREEN, YELLOW is an instance of class TrafficLightColor. They are represented as static and final, which in the Java world is considered as a constant.
  2. Each Enum value knows its index or position. So, the order in which Enum values are declared, matters. This is the same reason why when you iterate over the Enums through values() method, it will be iterated in the same order as specified in it’s declaration.

I hope we are good until here. We have gone more into the practical implementation of Enums in our other article. Click here to navigate

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.