Introduction to Java Enum data type with example programs

Introduction to Java Enum data type with example

An Enum in Java is a special data type that can be used to define a fixed set of constants. Similar to a class, an enum can contain constants, methods etc. The Java enum constants are implicitly public, static and final. Much like a class, an enum defines a type that can be used to define fields, variables and parameters. Enum is typically used for data sets where you know all possible values at compile time itself. In this post we will see an introduction to Java Enum data type with example programs.

Defining an Enum

An enum is defined similar a class.

In the Java, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:

As shown above, the enum constants are listed in the enum’s body (within the curly braces). While it is not necessary to define them in uppercase, it is common to write the constants in all uppercase letters to make it easier to recognize them.

The elements inside the Day enum are called enumeration constants.

Assigning an Enum

Once an enum is defined, you can assign its values to variables. Here is an example:

and here is how you can assign a value to the enum variable.

The day field is of type Day, therefore the only values that can be assigned to it are those constants that are defined in the Day enum.

Using an Enum

Here is an example Java program showing the usage of Day enum define earlier.

Here is the output of running the above program.

In can be noted in the above program that the enumeration constant in the switch case is used without their enumeration type name. This is due to the fact that the enum type is implicitly specified in the switch expression.

Iterating the enum constants

You can use the Enum’s values() method to get all of the enumeration constants. Here is an example code:

The above code will print the name of the enum constants in the same order as defined inside the Day enum.

The ordinal() method

This method is used to obtain an enumeration constant’s position in the list of constants. This is called the ordinal value. Here is an example code:

The output of the above code will be

Enums with Fields and methods

As said earlier an enum can contain fields and methods like a class. Here is an example program showing how to define fields and methods inside enum.

Below is the output of the above program.

Some points to remember

  • Just like a class, enums can also implement interfaces.
  • Constructor of enum type is private. So you cannot create an instance of enum by new keyword.
  • We can also have abstract methods inside enum
  • Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class.
  • If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;

 

That’s all about Introduction to Java Enum data type with example programs. If you have any doubt, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
4 comments

Add Comment

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