[JAVA] Can you set alias for ENUM names? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[JAVA] Can you set alias for ENUM names?

I have enum class containing various names. Each name has a int assigned for calculation purposed. How can I set an alias for the ENUM name so it prints the alias instead of the full name? I know it sounds stupid but I’m doing a project and replicating a program. For example: I have ENUMNAME(20) Is there a way to print this as ‘EN’?

15th Aug 2019, 2:08 AM
Raj Grewal
Raj Grewal - avatar
1 Answer
+ 4
First solution I can think of is to add a static method that returns a final string referring to the int value. Second solution: add a map field to the enum which contains the int, string pairs and return the map in a static method. Third: Override toString of your enum to return the alias.
6th Feb 2020, 8:44 PM
Tashi N
Tashi N - avatar