Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
An enumeration (enum) is a special type of class in Java that represents a group of constants (unchangeable variables). Enums are used when you need to represent a fixed set of values, such as days of the week or colors. They provide type safety, which means you can't assign an invalid value to an enum variable.
In this tutorial, we'll explore the basics of creating and using enums in Java.
To create an enum, use the enum
keyword followed by the name of the enumeration and a list of constant values enclosed in curly braces:
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
In the example above, we defined an enum called Day
with seven constants representing the days of the week.
You can use an enum just like any other class in Java. To create an enum variable, specify the enum type and assign one of its constant values:
public class EnumExample { public static void main(String[] args) { Day today = Day.WEDNESDAY; System.out.println("Today is " + today); } }
You can iterate through all the values of an enum using the values()
method, which returns an array of the enum's constants:
public class EnumExample { public static void main(String[] args) { for (Day day : Day.values()) { System.out.println(day); } } }
Enums work well with switch statements, allowing you to perform different actions based on the value of an enum variable:
public class EnumExample { public static void main(String[] args) { Day today = Day.WEDNESDAY; switch (today) { case MONDAY: System.out.println("It's Monday, the beginning of the week!"); break; case FRIDAY: System.out.println("It's Friday, the end of the week!"); break; case SATURDAY: case SUNDAY: System.out.println("It's the weekend!"); break; default: System.out.println("It's a weekday."); break; } } }
You can add methods and attributes to an enum to associate additional data with each constant:
enum Day { SUNDAY("Sun"), MONDAY("Mon"), TUESDAY("Tue"), WEDNESDAY("Wed"), THURSDAY("Thu"), FRIDAY("Fri"), SATURDAY("Sat"); private String abbreviation; private Day(String abbreviation) { this.abbreviation = abbreviation; } public String getAbbreviation() { return abbreviation; } } public class EnumExample { public static void main(String[] args) { for (Day day : Day.values()) { System.out.printf("%s (%s)\n", day, day.getAbbreviation()); } } }
In the example above, we added a constructor and a getAbbreviation()
method to the Day
enum. The constructor takes an abbreviation for each day, which is stored in the abbreviation
attribute.
In summary, enums in Java are a special type of class that represents a group of constants. They provide type safety and work well with switch statements. You can add methods and attributes to enums to associate additional data with each constant.
Creating and using Enums in Java
Enums in Java are a special data type that represents a fixed set of constants. Here's a basic example:
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Enum constants and values in Java
Enum constants are the predefined values within an Enum. You can access them using the dot notation:
Day today = Day.MONDAY;
Iterating through Enum values in Java
You can iterate through all values of an Enum using the values()
method:
for (Day day : Day.values()) { System.out.println(day); }
Using Enums in switch statements in Java
Enums are often used in switch statements for cleaner and more readable code:
switch (today) { case MONDAY: System.out.println("It's Monday!"); break; // Other cases... }
Enum constructors and methods in Java
Enums can have constructors and methods. Each constant can have its own implementation:
public enum Day { SUNDAY("Sun"), MONDAY("Mon"), /* ... */; private String abbreviation; private Day(String abbreviation) { this.abbreviation = abbreviation; } public String getAbbreviation() { return abbreviation; } }
Customizing Enums in Java
Enums can be customized with fields, methods, and even implementing interfaces:
public enum Day implements Displayable { SUNDAY("Sun"), MONDAY("Mon"), /* ... */; private String abbreviation; private Day(String abbreviation) { this.abbreviation = abbreviation; } @Override public String display() { return "Today is " + this.name(); } } interface Displayable { String display(); }
Enums and static methods in Java
Enums can have static methods, providing utility functions related to the Enum:
public enum Operation { ADD { public int apply(int x, int y) { return x + y; } }, SUBTRACT { public int apply(int x, int y) { return x - y; } }, /* ... */; public abstract int apply(int x, int y); } // Usage: int result = Operation.ADD.apply(5, 3);