Introduction To Enum In Java

Introduction

In this article we discuss Enum as a new feature of Java.

What is Enum?

It is a data type containng a fixed set of constants. It can be used for directions (North, South, East and West), for days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday), etcetera. The enum constants are static and final implicitly. It is available from Java 5. An Enum can be thought of as a class with a fixed set of constants.

Important points

Some important points about Enum are:

  1. may implement many interfaces but cannot extend any class because it internally extends the Enum class.
  2. can have fields, constructors, and methods
  3. it improves type safety
  4. can be easily used in a switch
  5. can be traversed

Example

class EnumEx1

  {

    public enum Seasonlist

      {

        SUMMER, FALL, WINTER, SPRING,

      }

    public static void main(String args[])

      {

        for (Seasonlist sl: Seasonlist.values())

        System.out.println(sl);

      }

  }

Output

Fig-1.jpg

Values() method of enum

The Java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum.

Java compiler generates some code automatically

The Java compiler automatically creates a final and static class that extends the Enum.

Defining Enum

The enum can be defined within or outside the class because it is similar to a class.

Example

enum Seasonlist

  {

   SUMMER, FALL, WINTER, SPRING,

  }

class EnumEx2

  {

    public static void main(String args[])

      {

        for (Seasonlist sl: Seasonlist.values())

        System.out.println(sl);

      }

  }

Output

fig-2.jpg

Initializing some specific value to constants

Constants of an enum can have some initial values, that start from 0, 1, 2, 3 and so on. But we can initialize a specific value of the enum by defining constructors and fields. As stated earlier, an enum contains constructors, methods and fields.

Example

The following example shows specification of an initial value to the enum constants.

class EnumEx3

  {

    enum Seasonlist

      {

         SUMMER(5), FALL(10), WINTER(15), SPRING(20);

         private int val;

         private Seasonlist(int val)

           {

             this.val=val;

           }

      }

    public static void main(String args[])

      {

        for (Seasonlist sl: Seasonlist.values())

        System.out.println(sl+" "+sl.val);

      }

  }

Output

fig-3.jpg

Applying enum on switch statement

We can also apply an enum on a switch statement, as in the following example:

Example

class EnumEx4

  {

    enum DayList

      {

        SUN, MON, TUE, WED, THU, FRI, SAT

      }

    public static void main(String args[])

      {

        DayList day=DayList.SUN;

        switch(day)

          {

            case SUN:

            System.out.println("you are choosing Sunday");

            break;

            case MON:

            System.out.println("You are choosing Monday");

            break;

            default:

            System.out.println("You are choosing other day");

          }

      }

  }

Output

fig-4.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all