Java enum
Enum stands for enumeration, it is a special type of class. This is used to set some predefined constants. The enum keyword is used to create an enumeration class.
The Enum in Java is a data type which contains a fixed set of constants. The enum keyword is used to create an enum in Java and separate the constants with a comma. This keyword has been introduced since Java 5. The enum in Java is more powerful than the other programming languages. As, we can define an enum either inside the class or outside the class. This feature is not available in c/c++ programming.
Java enum declaration syntax-enum <name> {
<enum_constant_1>,
<enum_constant_2>,
<enum_constant_3>,
...
<enum_constant_n>
<enum_constructor>
}
An enum type is a special data type that enables a variable to be a set of predefined constants. It can be used for directions (NORTH, SOUTH, EAST, and WEST), days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY), seasons (SPRING, SUMMER, WINTER, and AUTUMN or FALL), etc. As per the Java naming conventions, all the constants should be in capital letters.
Java enum examples-public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Rules for enum type in Java
- An enum type cannot be declared abstract or final.
- The Enum<E>is the direct superclass of an enum type.
- An enum constant specifies an instance of the enum type.
- An enum type can have constructors, methods, and variables just like a regular Java class.
- An enum constant can be optionally followed by a list of arguments and a class body.
- Enum constants are public, static, and final by default.
- An enum class can only implement interfaces.
- Enum constants are accessed using dot syntax.
Simple Java enum example
Here is a simple enum example where enum is declared inside a class. It defines the seasons, which can be FALL, WINTER, SPRING, or SUMMER.
public class EnumProgram{
//defining the enum
public enum Season { FALL,WINTER,SPRING,SUMMER }
//main method
public static void main(String[] args) {
//traversing the enum
for (Season sea : Season.values())
System.out.println(sea);
}
}
Output of the above code:
FALL
WINTER
SPRING
SUMMER
Java enum inside a class
Here, we declare an enum inside a class. It contains the days of the week as a set of constant values. Within the main method, we use the enum name to access the constant values.
public class EnumProgram {
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public static void main(String[] args) {
Day d = Day.FRIDAY;
System.out.println(d);
}
}
Output of the above code:
FRIDAY
Java enum in switch case
The switch statement is used when there is a need to match the value from many different cases and execute the statements within the matched case. Each value is known as a case. In the given Java program, we apply enum on switch statement. The variable Day is assigned with days of week. We pass the enum type as an argument to the switch statements within the main() method.
public class EnumProgram {
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public static void main(String[] args) {
Day dy = Day.FRIDAY;
switch (dy) {
case MONDAY:
System.out.println("Mondays are the start.");
break;
case FRIDAY:
System.out.println("Fridays are for fun.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are for rest.");
break;
default:
System.out.println("Middays are for work");
break;
}
}
}
Output of the above code:
Fridays are for fun.
Java enum loop through
Here, we use the for loop to iterate over on the enum values.
public class EnumProgram {
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public static void main(String[] args) {
for (Day dy : Day.values()) {
System.out.println(dy);
}
}
}
Output of the above code:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Java enum methods
There are some predefined methods of the enum class.
Java enum values() method
The values() method returns an array containing all the enum constants.
Day[] enumArray = Day.values();
Java enum valueOf() method
The valueOf() method returns the value of given constant enum.
Day.valueOf("SUNDAY")
// returns constant SUNDAY.
Java enum ordinal() method
The ordinal() method returns the index of the enum value.
ordinal(SUNDAY)
// returns 0
Java enum compareTo() method
The compareTo() method compares the enum constants based on their ordinal value.
Day.SUNDAY.compareTo(Day.SATURDAY)
Example of Java enum methods
public class EnumProgram {
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public static void main(String[] args) {
for (Day dy : Day.values()) {
System.out.println(dy);
}
System.out.println("Value of SUNDAY: "+Day.valueOf("SUNDAY"));
System.out.println("Index of SATURDAY: "+Day.valueOf("SATURDAY").ordinal());
System.out.println("Index of WEDNESDAY: "+Day.valueOf("WEDNESDAY").ordinal());
}
}
Output of the above code:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Value of SUNDAY: SUNDAY
Index of SATURDAY: 6
Index of WEDNESDAY: 3
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java