What is an enum ?
An enum is a kind of data type in java which can contain a fixed set of values. These values are constants. The enum can have any one value out of these fixed set of values.
Enum Syntax
An enum is created using keyword enum
followed by a user defined name and some fixed set of values enclosed between {
and }
.
Where is enum used
An enum is utilized when a particular type can have multiple fixed set of constant values. For Example, your application can have different types of user roles. Instead of using constants for these role types, an enum can be used as :
public enum ROLES {
ADMIN, MANAGER, USER;
}
Other examples where enum can be used are when defining days of week, months of year, math operators, seasons, planets of solar system etc.
Enum constants are accessed using dot(.) operator along with name of enum type just like static values are accessed using a class name such as ROLES.ADMIN
, ROLES.USER
etc.
Enum constants can be assigned to variables which are of the same enum type such as
ROLES role = ROLES.ADMIN;
Since enum constants can be assigned to values of same enum type, they can be easily utilized in switch-case
and if-else
constructs as shown below
How to use enum in switch statement
Below code shows how an enum can be used in a switch
statement. case statements should be the constants of the enum and value in switch should be one of the constant of enum. The case
statement which matches the enum value in switch
is executed.
public class DemonstrateEnum {
enum SEASON {
SUMMER, WINTER, AUTUMN, SPRING;
}
public static void main(String[] args) {
SEASON season = SEASON.AUTUMN;
switch (season) {
case SUMMER: {
System.out.println("Summer season");
break;
}
case WINTER: {
System.out.println("Winter season");
break;
}
case AUTUMN: {
System.out.println("Autumn season");
break;
}
case SPRING: {
System.out.println("Spring season");
break;
}
}
}
}
enum can be defined inside the class and outside the class as well. When defined inside the class, it can be accessed by other classes using the class name in which it is defined such as DemonstrateEnum.SEASON season
. When defined outside the class, enum can be accessed directly by its name.
enum
is defined outside the class, it should not have public
access. If it is assigned public
access modifier, it should be defined in its own file(similar to a class
).How to use enum in if-else
Similar to switch-case
statements, an enum can also be used in if-else
statements. Below code compares the value of an enum constant with all the constants of the enum. Since both the values are constants, they are compared directly by == operator.
public class DemonstrateEnum {
enum SEASON {
SUMMER, WINTER, AUTUMN, SPRING;
}
public static void main(String[] args) {
SEASON season = SEASON.AUTUMN;
if(season == SEASON.SUMMER) {
System.out.println("Summer season");
} else if(season == SEASON.WINTER) {
System.out.println("Winter season");
} else if(season == SEASON.AUTUMN) {
System.out.println("Autumn season");
} else if(season == SEASON.SPRING) {
System.out.println("Spring season");
}
}
}
How to iterate over Enum
Enum has a values
method. This method when called on enum returns the array of enum values which can be iterated just like a normal array using a loop as shown below
SEASON[] seasons = SEASON.values();
for (int i = 0; i < seasons.length; i++) {
SEASON season = seasons[i];
System.out.println(season.name());
}
The above code produces following output
SUMMER
WINTER
AUTUMN
SPRING
Constructors, fields and methods in enum
An enum can contain constructors, fields and methods just like a normal class. This constructor is automatically called when enum constants are declared. Remember you cannot call enum constructor yourself, it is called on its own. An enum can also contain methods if required.
Below code declares an enum along with a field, constructor which initializes this field and a method which returns the value of this field.
public enum Season {
SUMMER("summer"), AUTUMN("autumn"), WINTER("winter"), SPRING("spring");
/**
* enum field
*/
private String seasonStr;
/**
* Constructor
* @param s
*/
private Season(String s) {
seasonStr = s;
}
/**
* Method which returns the season string
* @return
*/
public String getSeason(){
return seasonStr;
}
public static void main(String[] args) {
Season[] seasons = Season.values();
for (Season season : seasons) {
// access the enum field using its method
System.out.println(season.getSeason());
}
}
}
private
access modifier.Let's tweak in
- If an enum contains fields and methods, the list of constants should end with a semi-colon(;).
- Enum constants should be declared at the very first line after enum declaration before any field, method or constructor declaration otherwise a compiler error will be generated.
- It is not mandatory to declare enum constants in upper case but it is considered as a standard.
- An enum cannot extend another enum. This is because every enum extends
java.lang.Enum
class and since java does not support multiple inheritance, it cannot extend any other class.