The enum
keyword in Java is used to define a special class that represents a group of constants. Enums are a powerful feature for creating a fixed set of related constants with type safety. Each enum constant is an instance of the enum class.
Table of Contents
- Introduction
enum
Keyword Syntax- Understanding Enums
- Examples
- Basic Enum
- Enum with Fields and Methods
- Using Enums in Switch Statements
- Real-World Use Case
- Conclusion
Introduction
Enums in Java are used to represent a fixed set of constants. They provide a way to define a collection of related values in a type-safe manner. Enums can also have fields, methods, and constructors, making them more powerful than simple constants.
enum Keyword Syntax
The syntax for defining an enum is as follows:
enum EnumName {
CONSTANT1,
CONSTANT2,
CONSTANT3
}
Example:
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
Understanding Enums
Enums can be used to define a collection of constants. Each constant is an instance of the enum type. Enums can also include fields, methods, and constructors to add more functionality.
Key Points:
- Enums provide type safety.
- Enums can have fields, methods, and constructors.
- Enums can implement interfaces but cannot extend classes.
- The
enum
constants are implicitlypublic
,static
, andfinal
.
Examples
Basic Enum
A basic example of an enum to represent days of the week.
Example
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public class Main {
public static void main(String[] args) {
Day today = Day.WEDNESDAY;
System.out.println("Today is: " + today);
}
}
Output:
Today is: WEDNESDAY
Enum with Fields and Methods
Enums can have fields, methods, and constructors to add more functionality.
Example
enum Day {
SUNDAY("Sun"),
MONDAY("Mon"),
TUESDAY("Tue"),
WEDNESDAY("Wed"),
THURSDAY("Thu"),
FRIDAY("Fri"),
SATURDAY("Sat");
private String abbreviation;
Day(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
public class Main {
public static void main(String[] args) {
Day today = Day.WEDNESDAY;
System.out.println("Today is: " + today);
System.out.println("Abbreviation: " + today.getAbbreviation());
}
}
Output:
Today is: WEDNESDAY
Abbreviation: Wed
Using Enums in Switch Statements
Enums can be used in switch statements for better readability and type safety.
Example
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public class Main {
public static void main(String[] args) {
Day today = Day.WEDNESDAY;
switch (today) {
case MONDAY:
System.out.println("Start of the work week.");
break;
case WEDNESDAY:
System.out.println("Midweek.");
break;
case FRIDAY:
System.out.println("End of the work week.");
break;
default:
System.out.println("Weekend.");
break;
}
}
}
Output:
Midweek.
Real-World Use Case
Defining Error Codes
Enums can be used to define a set of error codes in an application.
Example
enum ErrorCode {
NOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
BAD_REQUEST(400, "Bad Request");
private int code;
private String description;
ErrorCode(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
}
public class Main {
public static void main(String[] args) {
ErrorCode error = ErrorCode.NOT_FOUND;
System.out.println("Error Code: " + error.getCode() + ", Description: " + error.getDescription());
}
}
Output:
Error Code: 404, Description: Not Found
Conclusion
The enum
keyword in Java provides a powerful way to define a collection of constants with additional functionality. Enums are type-safe and can have fields, methods, and constructors. By understanding and using enums, you can create more readable and maintainable code, especially when dealing with fixed sets of related constants.