What is Java enumeration?

   Source: https://www.inc.com/marcel-schwantes/
Source: https://www.inc.com/marcel-schwantes/

If you have ever worked with any object-oriented programming (OOP) language like Java, then you probably have come across enumerations. Enumeration or enumerated data type in Java is a very common concept. It is often referred to as enums for short. This shot explores the concept of enums, when to use them, and how to use them.

What is enum in Java?

Enumeration in Java is a data type that consists of only constants, that is, values that are fixed and do not change. For example, the months in a year (January, February, etc.), days in week, seasons in a year, etc., can be declared as enums as they do not change.

Enums are implicitly static and final when used in Java. This means that they are available at class level and do not need to be instantiated to access them. Also, they cannot be changed once declared, hence it’s final. Enums implicitly extend the Java.Lang.Enum. package. Thus, they cannot extend another class as classes/enums in Java cannot have multiple inheritance.

How to create enums

The code snippet below shows how enums are declared in Java.

public enum UserType {
ADMIN, BUYER
}

As seen from the code above, enums are declared in a similar way to classes. The constants within the enums are each written in capital letters and separated by a comma. Enums can be defined within or outside a class. The code snippet below shows how they can be declared within a class.

public class EnumExample{
enum UserType {
ADMIN, BUYER
}
}

Just like classes, enums can also have fields, constructors, and methods. Some of the inert methods in enums are:

Values():

This returns all the constants within an enum in an array.

ValueOf():

This returns the value of a particular constant within an enum.

When to use enums

Enums should be used when the values you work with do not change. For instance, in the code above, the usertype could either be an admin or buyer, no more, no less. Hence, the usertype was declared as an enum. Enums help to achieve some level of abstraction and enable the developer to focus on the actual development of the business logic. Thus, enums are useful when abstraction is required. It also has the beautiful feature of being type safe.

How to iterate enums

The use of enums in switch statements has become a popular concept, although enums can be used in both switch statements and if/else statements. The code snippet below is an example of how they can be used in switch statements.

Public class Main{
public static void main(String[] args) {
Switch(UserType){
Case ADMIN:
System.out.println(“This is an admin”);
Break;
Case BUYER:
System.out.println(“This is a buyer”);
Break;
}
}
}

In a similar way, enums can be used in an if/else statement like in the code below.

Public class Main{
public static void main(String[] args) {
If (UserType == userType.admin){
System.out.println(“This is an admin”);
}
else{
System.out.println(“This is a buyer”);
}
}
}

As seen above, enumerations can be used in multiple ways like in switch and if/else statements. They can also be used with constructors, instance variables, and methods. The code snippet below sheds more light on this.

enum Airline
{
AirFrance(500),
Spirit(300),
Klm(550),
BritishAirways(450);
// variable
private int cost;
// Constructor
Airline(int pr )
{
cost = pr;
}
// method
int totalCost()
{
return cost;
}
}
class Main {
public static void main(String[] args)
{
for (Airline a : airline.values()){
System.out.println((a));
System.out.println( " : " + a.totalCost() + " pounds.");
}
}
}
//Output
// AirFrance
// : 500 pounds.
// Spirit
// : 300 pounds.
// Klm
// : 550 pounds.
// BritishAirways
// : 450 pounds.

In lines 28 - 31 in the code snippet above, we used a for loop to iterate through the values within the enum. Aside from for loops we can also use forEach, streams, enumset, etc., to loop through enums.

The concept of enums has been adapted to create enum maps and enumsets. An enum map is a kind of map where the keys are the constants within an enum. An enumset is a special type of set that is used to store enums.

In Java, when you deal with constants, the use of enums is encouraged as they have proven to be very useful. Hope this article has been useful. Enjoy coding!