The enum keyword in Java is used to define a set of named constants. It is a special data type that enables variable to be a set of predefined constants, making the code more readable and maintainable. Enums are commonly used to represent a collection of related constants, such as days of the week, months of the year, or states in a process.
Usage
Enums provide a way to define a collection of constants in a type-safe manner. They can include methods and fields, and they can implement interfaces.
CONSTANT1, CONSTANT2, CONSTANT3: The constants defined within the enum.
Examples
Example 1: Basic Enum
enum Day {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;}publicclassEnumExample{publicstaticvoidmain(String[] args){
Day today = Day.MONDAY;
System.out.println("Today is: "+ today);}}提供:
In this example, an enum Day is defined with constants representing the days of the week. The variable today is assigned the value Day.MONDAY, and the program prints the current day.
This example demonstrates an enum Size with a method getSize() that returns a string representation of the size. The variable size is assigned Size.MEDIUM, and the program prints the corresponding size description.
Example 3: Enum with Fields and Constructor
enum Planet {MERCURY(3.303e+23,2.4397e6),VENUS(4.869e+24,6.0518e6),EARTH(5.976e+24,6.37814e6);private final double mass;// in kilogramsprivate final double radius;// in metersPlanet(double mass, double radius){this.mass = mass;this.radius = radius;}public double getMass(){return mass;}public double getRadius(){return radius;}}publicclassEnumWithFieldsExample{publicstaticvoidmain(String[] args){for(Planet planet : Planet.values()){
System.out.printf("Planet: %s, Mass: %f, Radius: %f%n", planet, planet.getMass(), planet.getRadius());}}}提供:
In this example, the Planet enum has fields mass and radius, a constructor to initialize these fields, and getter methods to access them. The program iterates through all the enum constants and prints their details.
Tips and Best Practices
Use for Constants: Use enums to define a fixed set of constants, improving code readability and maintainability.
Type Safety: Enums provide type safety compared to traditional constant definitions using final static.
Switch Statements: Enums work seamlessly with switch statements, making the code cleaner.
switch(size){caseSMALL:// Handle small sizebreak;caseMEDIUM:// Handle medium sizebreak;caseLARGE:// Handle large sizebreak;}提供:
Implement Interfaces: Enums can implement interfaces, allowing for more flexible and reusable code.
interfaceDescribable{
String getDescription();}enum Color implementsDescribable{RED,GREEN,BLUE;
@Override
public String getDescription(){return"Color: "+this.name();}}提供:
Avoid Overuse: While enums are powerful, avoid overusing them for simple constants. Use them when there is a clear set of related constants.
The
enumkeyword in Java is used to define a set of named constants. It is a special data type that enables variable to be a set of predefined constants, making the code more readable and maintainable. Enums are commonly used to represent a collection of related constants, such as days of the week, months of the year, or states in a process.Usage
Enums provide a way to define a collection of constants in a type-safe manner. They can include methods and fields, and they can implement interfaces.
Syntax
EnumName: The name of the enum.CONSTANT1, CONSTANT2, CONSTANT3: The constants defined within the enum.Examples
Example 1: Basic Enum
In this example, an enum
Dayis defined with constants representing the days of the week. The variabletodayis assigned the valueDay.MONDAY, and the program prints the current day.Example 2: Enum with Methods
This example demonstrates an enum
Sizewith a methodgetSize()that returns a string representation of the size. The variablesizeis assignedSize.MEDIUM, and the program prints the corresponding size description.Example 3: Enum with Fields and Constructor
In this example, the
Planetenum has fieldsmassandradius, a constructor to initialize these fields, and getter methods to access them. The program iterates through all the enum constants and prints their details.Tips and Best Practices
final static.