articles

Home / DeveloperSection / Articles / Enumeration in Java

Enumeration in Java

Manoj Pandey2006 14-Apr-2015

In java have an enum keyword which represents a special data type that enables for a variable to belong to a set of predefined constants.The variable must be equal to one of the values that have been predefined for it.  An enum type is a special data type that enables for a variable to be a set of predefined constants.

The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.

Advantages of Enum-:

·         Enum improves type safety

·         Enum can be easily used in switch

·         Enum can be traversed

·         Enum can have fields, constructors and methods.

·         Enum may implement many interfaces but cannot extend any class because
it internally extends Enum class

Here I am creating a sample of Enum which help you to clear the concepts of Enumeration in java. 

public class Sample {

 

     public static void main(String args[]) {

 

           EnumExample days = new EnumExample(Day.MONDAY);

           days = new EnumExample(Day.TUESDAY);

           days = new EnumExample(Day.WEDNESDAY);

           days = new EnumExample(Day.THURSDAY);

           days = new EnumExample(Day.FRIDAY);

           days = new EnumExample(Day.SATURDAY);

           days = new EnumExample(Day.SUNDAY);

 

     }

}

 

enum Day {

     SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY

}

 

class EnumExample {

     Day day;

 

     public EnumExample(Day day) {

           this.day = day;

           tellItLikeItIs();

 

     }

 

     public void tellItLikeItIs() {

           switch (day) {

           case MONDAY:

         System.out.println("Mondays is first day of the week");

 

                break;

           case TUESDAY:

         System.out.println("Tuesday is the second day of the week");

 

                break;

           case WEDNESDAY:

         System.out.println("Wednesday is the third day of the week");

                break;

           case THURSDAY:

         System.out.println("Thursday is the fourth day of the week");

                break;

 

           case FRIDAY:

         System.out.println("Friday is the fifth day of the week");

                break;

           case SATURDAY:

         System.out.println("Saturday is the sixth day of the week");

                break;

           case SUNDAY:

         System.out.println("Sunday is the Last day of the Week");

                break;

 

           default:

                System.out.println("None of day");

                break;

           }

     }

}

 

Output-:

Mondays is first day of the week

Tuesday is the second day of the week

Wednesday is the third day of the week

Thursday is the fourth day of the week

Friday is the fifth day of the week

Saturday is the sixth day of the week

Sunday is the Last day of the Week

 

 


Updated 05-Dec-2017

Leave Comment

Comments

Liked By