Friday 2 May 2014

Enum Types

Enum type in java is used to define constants. By convention enum variable names are in capital letters.

Java provides enum keyword to define enum constants.

All enums implicitly extend java.lang.Enum.

enum Day{
   MONDAY,
   TUESDAY,
   WEDNESDAY,
   THURSDAY,
   FRIDAY,
   SATURDAY,
   SUNDAY
}

class EnumTest{
   public static void main(String args[]){
      System.out.print("Value for Monday is ");
      System.out.println(Day.MONDAY);
  
      System.out.print("Value for Tuesday is ");
      System.out.println(Day.TUESDAY);
  
      System.out.print("Value for Wednesday is ");
      System.out.println(Day.WEDNESDAY);
  
      System.out.print("Value for Thursday is ");
      System.out.println(Day.THURSDAY);
  
      System.out.print("Value for Friday is ");
      System.out.println(Day.FRIDAY);
  
      System.out.print("Value for Saturday is ");
      System.out.println(Day.SATURDAY);
  
      System.out.print("Value for Sunday is ");
   }
}

Output
Value for Monday is MONDAY
Value for Tueday is TUESDAY
Value for Wednesday is WEDNESDAY
Value for Thursday is THURSDAY
Value for Friday is FRIDAY
Value for Saturday is SATURDAY
Value for Sunday is SUNDAY

Example 2
class EnumTest1{

   static void messageOfTheDay(Day day){
      switch(day){
         case MONDAY:
            System.out.println("Today is Monday");
            break;
         case TUESDAY:
            System.out.println("Today is Tuesday");
            break;
         case WEDNESDAY:
            System.out.println("Today is Wednesday");
            break;
         case THURSDAY:
            System.out.println("Today is Thhursday");
            break;
         case FRIDAY:
            System.out.println("Today is Friday");
            break;
         case SATURDAY:
            System.out.println("Today is Saturday");
            break;
         case SUNDAY:
            System.out.println("Today is Sunday");
            break;  
      }
   }
 
   public static void main(String args[]){
      messageOfTheDay(Day.MONDAY);
      messageOfTheDay(Day.SATURDAY);
   }
}

Output
Today is Monday
Today is Saturday



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment