Return default value from valueof enum class


I have this enum:

public enum View {

I use valueOf to get constant enum:

var v = view.valueOf(someString);

In case that someString value is different from “aaa”, “bbb”, “ccc” I need the valueof will return default value “xxx”.

My question is how to return the default value for example “xxx” if input not equal to one of the strings mentioned above?


You could create your version of valueOf():

public enum View {

  public static View valueOfOrElse(String name) {
    for (View value : values()) {
      if ( {
        return value;
    return aaa;

Source: stackoverflow