Can we create an enum with custom values in java?
Enumeration (enum) in Java is a datatype which stores a set of constant values (Strings in general). You can use enumerations to store fixed values such as days in a week, months in a year etc.
Custom values to the constants
Instead of declaring just string constants in an enum, you can also have values to these constants as −
Whenever, you need to assign custom values to the constants of an enum −
- To hold the value of each constant you need to have an instance variable (generally, private).
- You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s).
- The initialization should be done only once. Therefore, the constructor must be declared private or default.
- To returns the values of the constants using an instance method(getter).
Example
In the following Java example, we are defining an enum with name Vehicles and declaring five constants representing the vehicle names with their prices as values.
enum Vehicles < //Constants with values ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000); //Instance variable private int price; //Constructor to initialize the instance variable Vehicles(int price) < this.price = price; >public int getPrice() < return this.price; >> public class EnumTest < public static void main(String args[]) < Vehicles vehicles[] = Vehicles.values(); for(Vehicles veh: vehicles) < System.out.println("Price of "+veh+" is: "+veh.getPrice()); >> >
Output
Price of ACTIVA125 is: 80000 Price of ACTIVA5G is: 70000 Price of ACCESS125 is: 75000 Price of VESPA is: 90000 Price of TVSJUPITER is: 75000
How can I declare enums using java
As for enums, there’re many other correct answers below by now (and a must read link above) . Just wanted to add that if your class holds some constants like in your example (I am not talking about enums right now. ) you can simply define interface this way: public interface TestConstants < String TEST = "test"; /*. */ >Interface ‘fields’ are implicitly public static.
@Arturs Licis: Using an interface to hold constants is an anti-pattern. An interface is meant to define behavior. Use a regular class with a private constructor to hold constants. Static imports can be used to access the constants without putting the class name before.
@JB Nizet: Completely agree, that’s why I said ‘I am not talking about enums right now. ‘ I just showed a more accurate way to declare ‘constants holder’ (interface versus class).
@Arturs Licis: My point is that you shouldn’t use an interface to hold constants, but a class: public class TestConstants rather than public interface TestConstants. If you completely agree with me, why do you give this advice to the OP?
6 Answers 6
public enum MyEnum < ONE(1), TWO(2); private int value; private MyEnum(int value) < this.value = value; >public int getValue() < return value; >>
In short — you can define any number of parameters for the enum as long as you provide constructor arguments (and set the values to the respective fields)
As Scott noted — the official enum documentation gives you the answer. Always start from the official documentation of language features and constructs.
Update: For strings the only difference is that your constructor argument is String , and you declare enums with TEST(«test»)
Extend Enums in Java
This tutorial demonstrates how to extend the enum functionality in Java.
Extend enum in Java
We can consider enum as a kind of compiler magic because, in the byte code, the enum is represented as a class with several static members and is inherited from abstract java.lang.Enum .
It is the reason the enum cannot extend any other class or enum . As we cannot extend enum in Java, it is also impossible for any class to extend an enum . Let’s learn by using the following example code and see what happens.
package delftstack; enum Cars Audi, BMW, Marcedes> public class Example extends Cars public static void main(String. args) > >
The code above has an enum named Cars , and class Example is trying to extend it. See output:
/Example.java:5: error: cannot inherit from final Cars public class Example extends Cars ^ /Example.java:5: error: enum types are not extensible public class Example extends Cars ^ 2 errors
As we can see, the class cannot extend the enum . So if it is impossible to extend the enum , can we still extend its functionality?
The functionality can be defined as the implemented methods in the enum . For example, the enum Cars from the above code can declare abstract methods for each member; see the example:
enum Cars Audi @Override public void drive() > >, BMW @Override public void drive() > >, Mercedes @Override public void drive() > >, ; public abstract void drive(); >
- If the enum belongs to a third-party library or another team, it will not allow to implementation of abstract methods.
- If it belongs to the module which doesn’t have the dependency required for the drive() method.
- If the enum is overloaded with other functions and data, it will be unreadable.
There are some solutions provided that can solve these problems and extend the functionality of enum in Java.
Solution 1: Mirror enum in Java
As the name suggests, we need to create another enum with the same data. That new enum will also implement the drive() method, So we have two enums now:
Example Code for enum Cars :
enum Cars Audi @Override public void drive() > >, BMW @Override public void drive() > >, Mercedes @Override public void drive() > >, ; public abstract void drive(); >
Example Code for enum DriveCars :
enum DriveCars Audi @Override public void drive() > >, BMW @Override public void drive() > >, Mercedes @Override public void drive() > >, ; public abstract void drive(); >
The second enum is the mirror of the first one. Now we can use both of these enums by extending the functionality; we need to use built-in enum methods that are name() and valueof() .
See the following example of how to use it:
Cars cars = . DriveCars.valueOf(cars.name()).drive();
The above code shows how enum Cars functionality is used in the enum DriveCars . Which means the functionality is extended.
The name() method in the above code is final , which cannot be overridden, and the valueOf method will be generated by the compiler. Both of these methods are a good fit for each other is there is no functional error in the extended operation.
There is one issue with the above code if the enum Cars is changed, the enum DriveCars will not have any idea, and it will cause the failure of the name and valueof trick. To solve this issue, we must let the DriveCars know that its parent mirror is Cars .
For that, we can use a static initializer to compare the DriveCars and Cars , which will throw the exception if both the enums do not match. Here is an example of that from the enumus library:
enum DriveCars . static Mirror.of(Cars.class); > >
The utility class will check if both enums match or not. This method will validate the name() and valueOf() trick.
Solution 2: Map enum in Java
If you don’t want to create another enum that holds only one method. In this case, we can use interface instead of the enum ; see the example below:
public interface Driver void drive(); >
Now to use this interface Drive with the enum Cars , we can create a mapping between them. Here is the example for the map:
MapCars, Driver> drivers = new EnumMap<>(Cars.class) put(Audi, new Driver() @Override public void driver()<>>) put(BMW, new Driver() @Override public void driver()<>>) put(Mercedes, new Driver() @Override public void driver()<>>) >>
Now to use them, use this simple piece of code:
The EnumMap used in the code above will guarantee that each enum member will appear only once, but it does not guarantee an entry for each member.
We can check the size of the map is equal to the number of members of enums:
drivers.size() == Cars.values().length
The enumus library also provides a utility for this case: if the map does not fit the Cars , it will throw the IllegalStateException . Here is the utility:
EnumMapValidator.validateValues(Cars.class, map, "Cars map");
Both methods above show how to make enums powerful by extending their functionality. Though it is impossible to directly extend an enum , we can use these tricks to extend their functionalities.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.