How to get value from Java enumerations
•
Java
I have an enumeration that looks like:
public enum Constants{ YES("y"),NO("N") private String value; Constants(String value){ this.value = value; } }
I have a test class that looks like
public class TestConstants{ public static void main(String[] args){ System.out.println(Constants.YES.toString()) System.out.println(Constants.NO.toString()) } }
The output is:
YES NO
replace
Y N
I'm not sure what's wrong here?
Solution
You need to override the toString method of enumeration:
public enum Constants{ YES("y"),NO("N") // No changes @Override public String toString() { return value; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码