title: Default value for ENUM
body: <p>I want to define a default value for an Enum class. The idea of my enum is to define a few specific <code>String</code> values and tie them to enumerations. However, if a user provide an <code>String</code> that I am not expecting, I want the enum to reflect an invalid state. Consider</p>
  
  <pre><code>  public class EnumDemo {
  
    public enum Food {
      HAMBURGER("h"), FRIES("f"), HOTDOG("d"), ARTICHOKE("a"), INVALID("invalid");
  
      Food(String code) {
        this.code = code;
      }
  
      private final String code;
  
      public String getCode() {
        return code;
      }
  
      public static Food fromString(String value) {
  
        return Arrays.stream(Food.values()).filter(s -&gt; s.code.equalsIgnoreCase(value)).findFirst()
            .orElse(Food.INVALID);
      }
    }
  
    public static void main(String[] args) {
      Food f1 = Food.fromString("h");
      System.out.println(f1 + " " + f1.getCode());
      f1 = Food.fromString("x");
      System.out.println(f1 + " " + f1.getCode());
    }
  
  }
  </code></pre>
  
  <p>this prints out </p>
  
  <pre><code>HAMBURGER h
  INVALID invalid
  </code></pre>
  
  <p>The problem here is that I am defining the string for the code. <code>invalid</code> is hardcoded as per</p>
  
  <p><code>INVALID("invalid")</code></p>
  
  <p>Is it possible to make this a variable? So that I can keep track of what the invalid input was? I tried </p>
  
  <p><code>INVALID(String x)</code></p>
  
  <p>but obviously got a syntax exception. Would it just be better not to use an enum? </p>
  
  <p>Lastly, the reason I want to keep track of <code>invalid</code> inputs is that in the future, I want the flexibility to change the enum depending on the users.</p>