Last Modified 2019.3.4

enum

Java enums were added in Java 5.

An enum type is a special data type that enables for a variable to be a set of predefined constants. The enum declaration defines a class (called an enum type). The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

package net.java_school.examples;

public enum Day {

	SUNDAY,
	MONDAY,
	TUESDAY,
	WEDNESDAY,
	THURSDAY,
	FRIDAY,
	SATURDAY

}
package net.java_school.examples;

public class EnumTest {
	Day day;

	public EnumTest(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) {
		EnumTest firstDay = new EnumTest(Day.MONDAY);
		firstDay.tellItLikeItIs();
		EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
		thirdDay.tellItLikeItIs();
		EnumTest fifthDay = new EnumTest(Day.FRIDAY);
		fifthDay.tellItLikeItIs();
		EnumTest sixthDay = new EnumTest(Day.SATURDAY);
		sixthDay.tellItLikeItIs();
		EnumTest seventhDay = new EnumTest(Day.SUNDAY);
		seventhDay.tellItLikeItIs();

		for (Day day : Day.values()) {
			System.out.println(day);
		}
	}

}
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

when there are fields and methods, the list of enum constants must end with a semicolon. The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

package net.java_school.examples;

public enum Day {

	SUNDAY("Sun"),//Pass the value "Sun" to the constructor
	MONDAY("Mon"), 
	TUESDAY("Tue"),
	WEDNESDAY("Wed"), 
	THURSDAY("Thu"), 
	FRIDAY("Fri"),
	SATURDAY("Sat");//Add a semicolon(;) at the end

	private final String value;

	//Add constructor
	private Day(String value) {
		this.value = value;
	}

	//Add method to get value
	public String getValue() {
		return value;
	}
}
package net.java_school.examples;

public EnumDayTest {
	public static void main(String[] args) {
		for (Day day : Day.values()) {
			System.out.println(day.getValue());
		}
	}
}	
Sun
Mon
Tue
Wed
Thu
Fri
Sat

The Eclipse project with source code for the examples is in the following zip file.
enum.zip

How to run

~/enum$ cd src/net/java_school/examples/
~/enum/src/net/java_school/examples$ javac -d ../../../../bin *.java
~/enum/src/net/java_school/examples$ cd -
~/enum$ java -cp ./bin net.java_school.examples.EnumTest
1.
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.

2.
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

3.
Sun
Mon
Tue
Wed
Thu
Fri
Sat
References