Enum for days of week in Java

Java 8Object Oriented ProgrammingProgramming

To set enum for days of the week, set them as constants

enum Days {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

Now create objects and set the above constants −

Days today = Days.Wednesday;
Days holiday = Days.Sunday;

The following is an example −

Example

 Live Demo

public class Demo {
   enum Days {
      Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
   }
   public static void main(String[] args) {
      Days today = Days.Wednesday;
      Days holiday = Days.Sunday;
      System.out.println("Today = " + today);
      System.out.println(holiday+ " is holiday");
   }
}

Output

Today = Wednesday
Sunday is holiday
raja
Published on 20-Dec-2018 09:36:35
Advertisements