Enums in Java – Exploring the Basics

Published Mar 30, 2017
Enums in Java – Exploring the Basics

In the world of Java, we often come across a situation where we want our method to have certain values as arguments. In other words, we want to restrict the values to set of predefined values. Well, this is where Enums come in.

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

So, here is the formal definition: Java allows 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. 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, users 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.

public enum TrafficLightColor {
  RED,
  GREEN,
  YELLOW
}
 
public void changeLight (TrafficLightColor trafficLightColor) {
  this.currentLight = trafficLightColor;
}

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:

changeLight(TrafficLightColor.RED);
changeLight(TrafficLightColor.YELLOW);
changeLight(TrafficLightColor.GREEN);

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)

public enum TrafficLightColor {
  RED,
  GREEN,
  YELLOW
}
 
// Can be accessed as TrafficLightColor.RED

2. Inside a class (TestOuter.java)

public class TestOuter {
  enum TrafficLightColor {
    RED,
    GREEN,
    YELLOW
  }
}

// Can be accessed as TestOuter.TrafficLightColor.RED;

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:

class TrafficLightColor {
  public static final TrafficLightColor RED = new TrafficLightColor(“RED”, 0);
  public static final TrafficLightColor GREEN = new TrafficLightColor(“GREEN”, 1);
  public static final TrafficLightColor YELLOW = new TrafficLightColor(“YELLOW”, 2);
  
  TrafficLightColor(String enumName, int index){
    // some lines of code
  }
}

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

The above snippet, however, has helped us infer a few more things about Enums:

  • Each of the enumerated values, RED, GREEN, and YELLOW is an instance of class TrafficLightColor. They are represented as static and final, which in the Java world is considered as a constant.

  • 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've been good and clear. We will go more into the practical implementation of Enums in my future article.

If you have any questions of suggestions, feel free to ping me on Codementor!

Discover and read more posts from Jimish Bhayani
get started
Enjoy this post?

Leave a like and comment for Jimish

3
Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer