---
title: "Java Enumerations: Days of the  Week Example"  
description: "In the previous post, we have seen why we need Enums in java, we have seen how to create integer patterns for Enumerations."  
author: "Simond Gear"  
published: 2016-05-26  
updated: 2017-12-18  
canonical: https://www.mindstick.com/articles/12129/java-enumerations-days-of-the-week-example  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Java Enumerations: Days of the  Week Example

In the previous post, we have seen why we need [Enums in java](https://www.mindstick.com/Articles/12128/java-enumerations-why-we-need-enums), 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.

---

Original Source: https://www.mindstick.com/articles/12129/java-enumerations-days-of-the-week-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
