Enum with Customized Value in Java Last Updated : 17 Jul, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Java // 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() ); } } } Output: name : RED action: STOP name : GREEN action: GO name : ORANGE action: SLOW DOWN Comment More infoAdvertise with us Next Article Java.lang.Enum Class in Java K kartik Improve Article Tags : Java Practice Tags : Java Similar Reads Java.lang.Enum Class in Java Enum class is present in java.lang package. It is the common base class of all Java language enumeration types. For information about enums, refer enum in java Class Declaration public abstract class Enum<E extends Enum> extends Object implements Comparable, Serializable As we can see, that En 8 min read Customize Java Annotation with Examples Java annotations are a mechanism for adding metadata information to our source code (Program). They are a powerful part of Java that was added to JDK5. Annotations provide an alternative to the use of XML descriptors. Also, we are able to attach them to packages, classes, interfaces, methods, and fi 3 min read Locale.Category values() method in Java with Examples The values() method of java.util.Locale.Category enum in Java is used to get the values of the constants of this Locale.Category enum type, in the order they are declared. This method returns an array of the constants and hence can also be iterated. Syntax: public static Locale.Category[] values() P 1 min read EnumMap entrySet() Method in Java The Java.util.EnumMap.entrySet() method in Java used to create a set out of the elements contained in the EnumMap. It basically returns a set view of the enum map. Syntax: enum_map.entrySet() Parameter: The method does not take any parameters. Return Value: The method returns the set view of mapping 2 min read Locale.Category valueOf() method in Java with Examples The valueOf() method of java.util.Locale.Category enum in Java is used to get the value of the specified constant of this Locale.Category enum type. This method takes the constant name as the parameter. Syntax: public static Locale.Category valueOf(String name) Parameter: This method accepts a param 2 min read Usage of Enum and Switch Keyword in Java An Enum is a unique type of data type in java which is generally a collection (set) of constants. More specifically, a Java Enum type is a unique kind of Java class. An Enum can hold constants, methods, etc. An Enum keyword can be used with if statement, switch statement, iteration, etc. enum consta 4 min read Like