The Power of Enums in Java

I hope you went through our previous article on the basics of Enums in Java. As we had already discussed that an Enum is a special kind of class, it can actually do more than just listing out the enumerated constant values. We can practically add following things with our Enums:

  1. Constructor
  2. Instance variables
  3. Methods
  4. And most importantly, the power of Enums: Constant Specific Class Body

Now, let us try to add some more functional aspect to our TrafficLightColor Enum that we had created in the previous article.

Let’s say we want to actually know the meaning of every Enum value, like for –

  1. RED – Stop
  2. GREEN – Go
  3. YELLOW – Warning for changing Signal

In a conventional way, what we can do is, to have some mapping table kind of data structure (HashMap or others). But that will lead to poor design in terms of maintenance and understanding.

But here we have our savior:  Enums!

Simplest way is to treat the Enum values (RED, GREEN, YELLOW) as objects, and objects definitely can have their own instance variables. So, we can assign our desired values to each instance of the Enum by passing an argument to the Enum Constructor.

Let’s analyse the code below:

Running the above application will provide us with the following output:

RED — Stop

I hope we are good until now. Ok, so let’s move on. Finally, here comes the beauty of Enums: Constant Specific Class Body.

Let’s us imagine one hypothetical scenario that TrafficLightColor Enums have timings attached to it as well, in addition to the meanings that we have already added above successfully.

Typically RED and GREEN both have the same timing of say 30 seconds, but as YELLOW is only for warning, it blinks for say 4 seconds. How can we implement this through Enum? Let’s see.

So, we can have a method called getTime() which returns a default time of 30, but for YELLOW it should return 4. Conventionally, we can put some if-else logic and achieve our functionality but that will be difficult to maintain.

So, basically what we want is an ability to override the default behavior of method getTime() for the constant YELLOW.

Below is how we can achieve it:

Note that, the code written within the curly braces of YELLOW constant is called Constant Specific Class Body, which overrides the default behavior of the getTime() method in the TrafficLightColor Enum.

Let’s test out the above changes with our TrafficLightColorTester class:

Running the above class will provide us with the following output:

RED — Stop
YELLOW — Warning for changing Signal
RED — 30

I hope we have much more than just introduced you to Enums and you probably might now be more comfortable picturing out the things with Enums. The above discussed scenarios particularly illustrate the power of Enums.

Let’s have a quick recap about Enums:

  1. Enums are a special type of Java Class.
  2. Enums are nothing but an enumerated list of constants.
  3. They are useful for cleaner and bug-free code.
  4. Enums can be declared independently or within the class body, but cannot be declared inside a method.
  5. Enums can have constructors, instance variables and methods as well.
  6. Enums can have multiple argument constructors and even overloaded constructors.
  7. And finally, each Enum constant has the ability to override the default behavior of a method defined in the respective Enum, by having Constant Specific Class Body.

Kindly share your views & suggestions, so we can improve/update anything that we might have messed/missed 😉

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.