Java Program to Create an enum class
What is enum in Java ?
In Java, an enum is a special type of data type that defines a set of predefined constants. Enums are used to create a fixed set of options that can be used in a program. They are similar to a class, but are used to define a set of constants that cannot be changed. Enums are defined using the “enum” keyword and can be used in a variety of ways in a program, such as in a switch statement or as an attribute for a class.
Enums in Java :
Enums in Java are a powerful feature that provide a way to define a fixed set of values that a variable can take on. Enums are similar to classes and can have constructors, methods and fields like classes. Enums are typically used to represent a set of predefined values, such as the days of the week, or the suits in a deck of cards.
One of the main advantages of using enums is that they provide a way to define a fixed set of values that can be used throughout a program. This can help prevent bugs caused by using the wrong value or typo. Enums also provide type safety, as the compiler will check that the correct enum type is being used.
To construct an enum in Java, you can follow these steps:
- Use the “enum” keyword to define a new enum type. The name of the enum should be in uppercase letters.
- Declare the constants that the enum will represent. Each constant should be separated by a comma.
- Optionally, you can include a constructor, methods and fields within the enum.
enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }In this example, the enum “Days” is defined and the constants “MONDAY”, “TUESDAY”, “WEDNESDAY”, “THURSDAY”, “FRIDAY”, “SATURDAY” and “SUNDAY” are declared.
You can also define an enum with a constructor, methods and fields :
enum Days { MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); private final String fullName; private final int value; private static int nextValue = 0; Days(String fullName) { this.fullName = fullName; this.value = nextValue++; } public String getFullName() { return fullName; } public int getValue() { return value; } }
In this example, the enum “Days” is defined with a constructor that takes a full name of the day, fields fullName, value and a static field nextValue. Also it has two methods getFullName and getValue.
You can also create an instance of the enum using the enum constants as :
Days today = Days.MONDAY; System.out.println("Today is : "+today);
Note : that the enum constants are always in uppercase.
Example 1 :
enum Size {
SMALL("S"), MEDIUM("M"), LARGE("L");
private String abbreviation;
private Size(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
public class Main {
public static void main(String[] args) {
Size s = Size.SMALL;
System.out.println("Size: " + s);
System.out.println("Abbreviation: " + s.getAbbreviation());
}
}
Output :
Size: SMALL Abbreviation: S
Explanation:
The enum class defines a set of enumerated values for a variable. In this case, the Size class defines three enumerated values: SMALL, MEDIUM, and LARGE. Each enumerated value has an associated abbreviation, which is passed as an argument in the constructor of the enumerated value.
The Main class contains the main method, which is the starting point of the program. In the main method, an object of the Size enum is created and assigned the value Size.SMALL. Then, the println method is used to print the value of the Size object and the abbreviation of the enumerated value, which is obtained using the getAbbreviation method.
Example 2 :
enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public class Main {
public static void main(String[] args) {
Days today = Days.MONDAY;
if (today == Days.SATURDAY || today == Days.SUNDAY) {
System.out.println("It's the weekend!");
} else {
System.out.println("It's a weekday.");
}
}
}
Output :
It's a weekday
Explanation:
This code defines an Enum class Days with seven constants, one for each day of the week. In the Main class, it creates a variable today and assigns it the value Days.MONDAY. Then, it checks whether today is Days.SATURDAY or Days.SUNDAY using an if-else statement and prints the corresponding message. This example shows how to use an enumeration class to represent a set of predefined values, like days of the week, in a program.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment