Everything about Enums in Java
Everything about Enums in Java
👋 Welcome, Java enthusiasts, to our first post at Java Interview Pro! Today, we'll embark on an exciting journey to explore everything about Enums in Java. Whether you're new to enums or need a refresher, this guide will equip you with the knowledge to leverage enums effectively in your code. 🎉
What are Enums?
In Java, enums (short for enumeration types) are special classes that define a fixed set of constants. They offer a powerful way to represent a group of related named values, making code more readable and maintainable. Enums ensure type safety and restrict values to only those defined within the enum class.
Enums were introduced in Java 5 (JDK 5) and are a powerful way to represent a fixed set of related constants in a type-safe manner.
Each constant within the enum represents a unique value of that enum type.
Enum Declaration and Usage:
To declare an enum in Java, you use the enum keyword followed by the constant names, each separated by a comma. Each constant is defined as a public static final instance of the enum type.
Let's start by declaring an enum and exploring its usage. In this example, we'll create an enum for days of the week and use it in a simple code snippet:
public class Main {
public enum DayOfWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
DayOfWeek today = DayOfWeek.MONDAY;
System.out.println("Today is " + today);
}
}Each constant is implicitly declared as a public static final instance of the DayOfWeek enum type, meaning they are constants that can be accessed from anywhere in the code. For example, you can use them in a switch statement.
DayOfWeek today = DayOfWeek.MONDAY;
switch (today) {
case MONDAY:
System.out.println("It's Monday!");
break;
case TUESDAY:
System.out.println("It's Tuesday!");
break;
// and so on for other days
default:
System.out.println("It's some other day.");
break;
}Enum Methods and Properties:
Enums come with built-in methods, such as name(), ordinal(), and values(), to facilitate working with enum constants. We'll demonstrate these methods in another example:
public class Main {
public enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
public static int getNumberOfMonths() {
return values().length;
}
}
public static void main(String[] args) {
for (Month month : Month.values()) {
System.out.println(month.name() + " is month number " + month.ordinal());
}
System.out.println("Total number of months: " + Month.getNumberOfMonths());
}
}Output:
JANUARY is month number 0
FEBRUARY is month number 1
...
DECEMBER is month number 11
Total number of months: 12Enums with Custom Properties:
Enums can also have fields, constructors, and methods. For example, let's add a field and a method to the DayOfWeek enum:
public class Main {
public enum DayOfWeek {
SUNDAY("Sun"),
MONDAY("Mon");
// other days...
private final String abbreviation;
private DayOfWeek(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
public static void main(String[] args) {
DayOfWeek today = DayOfWeek.MONDAY;
System.out.println("Today's abbreviation: " + today.getAbbreviation()); // Output: "Today's abbreviation: Mon"
}
}Output:
Today's abbreviation: MonEnums are a valuable feature in Java that simplifies and enhances your code's clarity and safety. By using enums, you can represent fixed sets of constants effectively, utilize built-in methods, and even create enums with custom properties. 🌟
That's all for our comprehensive guide to enums in Java! We hope you found this post helpful and informative. Don't forget to subscribe to Java Interview Pro for more Java insights, interview tips, and code snippets. Happy coding! 💻

