How to get value from a Java enum

后端 未结 5 2067
旧时难觅i
旧时难觅i 2021-01-02 01:49

I have a enum which looks like:

public enum Constants{
  YES(\"y\"), NO(\"N\")
  private String value;

  Constants(String value){
    this.value = value;
           


        
5条回答
  •  悲&欢浪女
    2021-01-02 02:11

    You can also add a getter to the enumeration and simply call on it to access the instance variable:

    public enum Constants{
        YES("Y"), NO("N");
        private String value;
    
        public String getResponse() {
            return value;
        }
    
        Constants(String value){
            this.value = value;
        }
    }
    
    public class TestConstants{
        public static void main(String[] args){
            System.out.println(Constants.YES.getResponse());
            System.out.println(Constants.NO.getResponse());
        }
    }
    

提交回复
热议问题