Enums

Enums #

In Java enum is a special class that contains special type of constants. Constants in the enum should be seperated with comma and they should be written in all upper case.

enum Season {
	SPRING,
	SUMMER,
	AUTUMN,
	WINTER
}

An enum constant can be accessed with dot syntax.

Season myVariable = Season.WINTER;

Enum with Switch Statement #

Enums are commonly used with switch statements.

enum Season {
	SPRING,
	SUMMER,
	AUTUMN,
	WINTER
}

public class Main {
	public static void main(String[] args) {
		Season myVariable = Season.WINTER;

		switch(myVariable) {
			case SPRING:
				System.out.println("There are flowers.");
				break;
			case SUMMER:
				System.out.println("It is sunny.");
				break;
			case AUTUMN:
				System.out.println("Leafs are falling.");
				break;
			case WINTER:
				System.out.println("It is snowing.");
				break;
		}
	}
}

The output of the code above will be It is snowing..