Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it. 1. How …
Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it.
1. How to Create Enum with Multiple Values
The syntax to create an enum with multiple values is very similar to the syntax of enum with a single value assigned to it. we should do the following steps to have an enum with different values:
Create enum constructor which accepts multiple values
Assign each constructor argument to a member field in the enum definition
Create getter methods so we can access any of the values assigned to a particular enum constant
Create a reverse lookup method so we can get the enum constant from any given enum value assigned to it
2. Example of Enum with Multiple Values
In the given example, we are creating an enum AccountStatus. In our application, we can denote an account’s status with any of the multiple values.
For instance, we can indicate an active status with the strings “A“, “active" and even an integer value 1. Similarly, other statuses can have multiple values assigned to them.
Let’s see how to use this enum in our application. We will first list down all the statuses available in the application. Then we will apply reverse lookup to see what enum constant is associated with value 0 or string “A“.
EnumWithMultipleValues.java
importjava.util.Arrays;importjava.util.Optional;publicclassEnumWithMultipleValues{publicstaticvoidmain(String[] args){//Print all enum and valuesfor(AccountStatus as :AccountStatus.values()){System.out.println("Status "+ as.getCode()+" is : "+ as.getFullName());}//Reverse Lookup ExamplesOptional<AccountStatus> statusEnum
=AccountStatus.getAccountStatusByValue(0);if(statusEnum.isPresent()){System.out.println("Account Status Full Name: "+ statusEnum.get().getFullName());System.out.println("Account Status Short name: "+ statusEnum.get().getShortName());}Optional<AccountStatus> activeStatusEnum
=AccountStatus.getAccountStatusByValue("A");if(statusEnum.isPresent()){System.out.println("Account Status Full Name : "+ activeStatusEnum.get().getFullName());System.out.println("Account Status Code : "+ activeStatusEnum.get().getCode());}}}
Program output.
Terminal
Status -1 is : purged
Status 0 is : inactive
Status 1 is : active
Account Status Full Name: inactive
Account Status Short name: I
Account Status Full Name : active
Account Status Code :1
Happy Learning !!
Comments
Subscribe
4 Comments
Most Voted
NewestOldest
Inline Feedbacks
View all comments
Lokesh Gupta
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments