Enum with Customized Value in Java
Prerequisite : enum in Java
By default enums have their own string values, we can also assign some custom values to enums. Consider below example for that.
Examples:
enum Fruits
{
APPLE(“RED”), BANANA(“YELLOW”), GRAPES(“GREEN”);
}
In above example we can see that the Fruits enum have three members i.e APPLE, BANANA and GRAPES with have their own different custom values RED, YELLOW and GREEN respectively.
Now to use this enum in code, there are some points we have to follow:-
- We have to create parameterized constructor for this enum class. Why? Because as we know that enum class’s object can’t be create explicitly so for initializing we use parameterized constructor. And the constructor cannot be the public or protected it must have private or default modifiers. Why? if we create public or protected, it will allow initializing more than one objects. This is totally against enum concept.
- We have to create one getter method to get the value of enums.
enum TrafficSignal
{
RED( "STOP" ), GREEN( "GO" ), ORANGE( "SLOW DOWN" );
private String action;
public String getAction()
{
return this .action;
}
private TrafficSignal(String action)
{
this .action = action;
}
}
public class EnumConstructorExample
{
public static void main(String args[])
{
TrafficSignal[] signals = TrafficSignal.values();
for (TrafficSignal signal : signals)
{
System.out.println( "name : " + signal.name() +
" action: " + signal.getAction() );
}
}
}
|
Output:
name : RED action: STOP
name : GREEN action: GO
name : ORANGE action: SLOW DOWN
Last Updated :
17 Jul, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...