Enum Types

An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type’s fields are in uppercase letters.

In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:

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

Example is as :

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

public class EnumTypes {
	Day day;

	public EnumTypes(Day day) {
		this.day = day;
	}

	public void tellItLikeItIs() {
		switch (day) {
		case MONDAY:
			System.out.println("Mondays are bad.");
			break;

		case FRIDAY:
			System.out.println("Fridays are better.");
			break;

		case SATURDAY:
		case SUNDAY:
			System.out.println("Weekends are best.");
			break;

		default:
			System.out.println("Midweek days are so-so.");
			break;
		}
	}

	public static void main(String[] args) {
		EnumTypes firstDay = new EnumTypes(Day.MONDAY);
		firstDay.tellItLikeItIs();
		EnumTypes thirdDay = new EnumTypes(Day.WEDNESDAY);
		thirdDay.tellItLikeItIs();
		EnumTypes fifthDay = new EnumTypes(Day.FRIDAY);
		fifthDay.tellItLikeItIs();
		EnumTypes sixthDay = new EnumTypes(Day.SATURDAY);
		sixthDay.tellItLikeItIs();
		EnumTypes seventhDay = new EnumTypes(Day.SUNDAY);
		seventhDay.tellItLikeItIs();
	}
}