articles

Home / DeveloperSection / Articles / Java Enumerations: Days of the Week Example

Java Enumerations: Days of the Week Example

Simond Gear 5362 26-May-2016

In the previous post, we have seen why we need Enums in java, we have seen how to create integer patterns for Enumerations. But this pattern poses many problems, they are not typesafe and for each constant we need to provide prefix for them. Also, these patterns are brittle in sense and for every change in patterns we need to compile the program again. And, if we print these patterns we only get integer values which don’t make much sense. 


To overcome all this we use enum construct in java and we will learn this construct with the simplest example by implementing Days of the Week Enums.

Suppose we want to create a list of the days in a week. We would do so with the following declaration:

enum WeekDays {

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

}

 

This declaration assigns integer ordinal values to the constants. Thus,

·         MONDAY takes a value of 0

·         TUESDAY takes a value of 1

·         And so on…

If we try to print a list of days in a for loop, the output would simply list these ordinal values. With the new facilities available in enum, we can overcome this problem easily by adding an overloaded toString method to the declaration. This can be seen here:

public class WeekDaysList {

                     public static void main(String[] args) {
                             System.out.println("Days of week:");
                             for (DaysOfTheWeek day : DaysOfTheWeek.values()) {
                                      System.out.printf("%s ", day);
                             }
                             System.out.println();
                     }

}
enum DaysOfTheWeek {
                            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;                      @Override
                     public String toString() {
                             // only capitalize the first letter
                             String s = super.toString();
                             return s.substring(0, 1) + s.substring(1).toLowerCase();

                     }

}

 

The DaysOfTheWeek enumeration, after declaring the seven constants pertaining to the days of the week, adds a definition of the overloaded toString method.

Here, we obtain the string representation of each constant by calling super.toString. We then retain the first letter of the string and convert the rest into lowercase. The modified string is returned to the caller.

 In the main function, we iterate through all the elements of the enum using a foreach loop:

for (DaysOfTheWeek day : DaysOfTheWeek.values()) {
                      System.out.printf("%s ", day);

}


 
The values method returns an array that contains a list of enumeration constants. When we print the element day, its toString method is called by default. Our overridden toString method prints each constant in its string format with only the first letter capitalized.

Output

The program output is shown here:

Days of week:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday


This takes care of one of the problems mentioned earlier—the string representation of each enumeration constant is returned rather than its ordinal number (an integer).

In the next post, we will customize our Enumeration by adding properties to it.


Updated 18-Dec-2017

Leave Comment

Comments

Liked By