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:-

  1. 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.
  2. We have to create one getter method to get the value of enums.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate how values can
// be assigned to enums.
enum TrafficSignal
{
    // This will call enum constructor with one
    // String argument
    RED("STOP"), GREEN("GO"), ORANGE("SLOW DOWN");
  
    // declaring private variable for getting values
    private String action;
  
    // getter method
    public String getAction()
    {
        return this.action;
    }
  
    // enum constructor - cannot be public or protected
    private TrafficSignal(String action)
    {
        this.action = action;
    }
}
  
// Driver code
public class EnumConstructorExample
{
    public static void main(String args[])
    {
        // let's print name of each enum and there action
        // - Enum values() examples
        TrafficSignal[] signals = TrafficSignal.values();
  
        for (TrafficSignal signal : signals)
        {
            // use getter method to get the value
            System.out.println("name : " + signal.name() +
                        " action: " + signal.getAction() );
        }
    }
}

chevron_right


Output:

name : RED action: STOP
name : GREEN action: GO 
name : ORANGE action: SLOW DOWN 

This article is contributed by Vihang Shah. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : rss009



Article Tags :
Practice Tags :


30


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.