+ 5

Printing out a specified enum value in Java

Could you please give me a hint on printing out enum values in Java? I would like to print a particular value prescribed for a particular enum constant. For instance, there are enum constants with customised values: RED(20), BLUE(2), GREEN(0) and I need to print only the values 2 and 0. Is there any way I can do it?

16th Jan 2019, 4:10 PM
ZdzichPych
ZdzichPych - avatar
2 odpowiedzi
+ 10
Java Program to print custom String values to Enum in Java there are two ways you can associate a user-defined String value to an enum constant By overriding toString() methodBy Using an instance variable In our example, we have an enumeration type called Color, it has three enum constants RED, GREEN,  and BLUE.  By default when you pass any of these enumeration constants to System.out.println() method, it will call toString() method of java.lang.Object. Since every enum by default extends java.lang.Enum, which overrides toString() method. Read more: http://www.java67.com/2014/12/2-ways-to-print-custom-string-value-of.html#ixzz5co8xLVmN
16th Jan 2019, 9:21 PM
AKS
AKS - avatar
+ 2
ADEVIL, thank you very much for your answer, I will certainly use your advice
17th Jan 2019, 8:51 AM
ZdzichPych
ZdzichPych - avatar