In Java, an enum is a special class type that represents a collection of constants (unchangeable variables, such as final variables). Java enums are more powerful than simple constants, as they can have fields, methods, and constructors.
An enum type is a reference data type that can be used to define a collection of constants (like MONDAY, TUESDAY, SUNDAY), which improves code readability, maintainability, and type safety.
Characteristic of Enum
1. Enums are type-safe: This means the compiler checks the values, preventing invalid values from being assigned.
2. Enums can have fields, methods, and constructors: Enums are more powerful than just simple constants; they can contain behaviors and properties.
3. Enums can be iterated: You can loop through all enum values easily.
Syntax:
enum EnumName {
CONSTANT1, CONSTANT2, CONSTANT3;
}
Explain:
- EnumName is the name of the enum.
- CONSTANT1, CONSTANT2, CONSTANT3 are the constants.
Example:
// Main.java file
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class Main {
public static void main(String[] args) {
Day today = Day.TUESDAY;
System.out.println("Today is: " + today);
}
}
Explanation:
- Day is an enum representing the days of the week.
- Day.TUESDAY is used to assign the constant TUESDAY from the Day enum to the variable today.
Output:
Enum with values() Method
You can use the values() method to get all the constants of an enum and loop through them.
Example:
// Main.java file
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class Main {
public static void main(String[] args) {
// Loop through all enum constants
for (Day day : Day.values()) {
System.out.println(day);
}
}
}
Output:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Enum with ordinal() Method
The ordinal() method returns the position of the constant
// Main.java file
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class Main {
public static void main(String[] args) {
// Using ordinal() method to get position
System.out.println("Position of TUESDAY: " + Day.TUESDAY.ordinal()); // Output: 2
}
}
Output:
Using Enum with a Switch Statement
Using an enum with a switch statement is a common and powerful way to handle different cases based on enum constants. The switch statement allows you to execute different blocks of code depending on the value of the enum.
Example:
// Main.java file
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class Main {
public static void main(String[] args) {
Day today = Day.TUESDAY; // Set today as TUESDAY
// Using a switch statement with the enum Day
switch (today) {
case SUNDAY:
System.out.println("It's Sunday, time to relax!");
break;
case MONDAY:
System.out.println("It's Monday, let's start the week strong!");
break;
case TUESDAY:
System.out.println("It's Tuesday, keep going!");
break;
case WEDNESDAY:
System.out.println("It's Wednesday, you're halfway there!");
break;
case THURSDAY:
System.out.println("It's Thursday, almost the weekend!");
break;
case FRIDAY:
System.out.println("It's Friday, time to finish strong!");
break;
case SATURDAY:
System.out.println("It's Saturday, enjoy your day off!");
break;
default:
System.out.println("Invalid day!");
break;
}
}
}
Output: