Java Enum Example

By | September 29, 2021
Java Enum Example

Introduction

Java Enums are used to define constants. Rather than declaring common categories of constants as individual strings, we can categorize and declare them as Enums. In this article, we will discuss how to declare Enums, use Java Enum in a for loop and Java switch case, and how Java Enums can be used in if/else statements.

Some common examples of enums are days of the week, months of the year, directions, card types of a deck, options in a drop-down list like engineering subject, states of a country, etc.

Vamware

Other than these, projects can have their own specific codes for various items. For example, a gardening website can have flower names defined using enums.

How to declare simple Java enum type

Enums are simple to declare and use. The syntax to declare enum is similar to creating a Java class. Remember that enums cannot be declared locally inside methods because they are just like a class definition. The most common example of an enum is that of days of a week:

public enum Weekday
{
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
}
public enum Weekend
{
SATURDAY, SUNDAY
}

Same way, suppose your project requires different weather conditions as constants. This can be put as an enum:

public enum Weather{
SUMMER, WINTER, RAINY, AUTUMN, SPRING, FALL
}

You can use enums even to describe margins for a CSS object:

public enum CSSMargin{
LEFT, RIGHT, TOP, BOTTOM
}

How to use Java enum

Enums can be used anywhere in the program. They can also be used in switch-case, if-else, or for loops. To get a single value of enum, we use the valueOf() method, and to get the entire array, we use the values() method.

Weather rainy = Weather.valueOf("RAINY");
System.out.println(rainy);
Weather[] weather = Weather.values();
System.out.println(Arrays.toString(weather));

Note that to print the value of the Weather array, we use Java.util.Arrays.toString() method.

String spring = "SPRING";
if(Weather.SPRING.toString().equals(spring))
System.out.println("Yay! Its Spring!");

Since Weather.SPRING is an enum and not a string, we convert it to string using the toString() method before the comparison.

Using Java enum in for loop

We can use enum in a for loop:

int i = 0;
for (Weekday wkd: Weekday.values())
    {
      System.out.println("Weekday " + i++ + ": " + wkd);
    }

This will print all the values present in the Weekday enum one by one.

Using Java enum in switch statement

public static String getColorCode(ColorCodes cc){

switch(cc){
case RED: return "#FF0000";
case BLUE: return "#0000FF";
case YELLOW: return "#FFFF00";
case GREEN: return "#008000";
case BLACK: return "#000000";
case WHITE: return "#FFFFFF";
default: return null;
}
}

Now, you can call this method in the main method of your class:

System.out.println(getColorCode(ColorCodes.GREEN));

This will print the color code of the enum GREEN, #008000.

Using Java enum in if/else statement

We have seen above how we use an enum in the ‘if’ statement by converting it to a string. Here is an example of comparing two enums:

if(rainy == Weather.RAINY)
System.out.println("Oops, it's rainy out there!");

where rainy is the enum of type Weather.

Conclusion

We have discussed simple example usages of the enum data type. Enums can also have methods and other fields inside their definition. Check out the Java Enum official documentation page on the example of the Java enum Planets, which contains methods and fields to describe the planets.

People are also reading: 

Leave a Reply

Your email address will not be published. Required fields are marked *