Thursday, May 2, 2013

Java / Enum type


Enum type is a data type that enables for a variable to be a set of predefined constants.
The variable must be equal to one of the values that have been predefined for it.
Because they are constants, the names of an enum type's fields are in uppercase letters.
enum WeekDays 
{
 MONDAY, THUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

public class MyEnum
{
 WeekDays weekDays;
 
 public MyEnum(WeekDays aWeekDays) 
 {
  this.weekDays = aWeekDays;
 }
 
 public void showCommentForEachDay()
 {
  if( weekDays.equals(WeekDays.SATURDAY) || weekDays.equals(WeekDays.SUNDAY) )
   System.out.println("I like weekends");
  else
   System.out.println("Waiting for weekend... it's " + this.weekDays);
 }
 
 public static void main(String[] args)
 {
  MyEnum me = new MyEnum(WeekDays.SATURDAY);
  me.showCommentForEachDay(); /* out: I like weekends */
 }
}

No comments: