get enum name from enum value [duplicate]

I've read a lot about how obtain the corresponding name of an enum from its value using java, but no example seems to work for me! What is wrong?

public class Extensions {


    public enum RelationActiveEnum
    {
        Invited(0),
        Active(1),
        Suspended(2);

        private final int value;

        private RelationActiveEnum(final int value) {
            this.value = value;
        }
    }

}

and in another class I use:

        int dbValue = supp.ACTIVE;
        Extensions.RelationActiveEnum enumValue(dbValue);
        String stringName = enumValue.toString(); //Visible
        // OR
        int dbValuee = supp.ACTIVE;
        String stringValue = Enum.GetName(typeof(RelationActiveEnum), dbValue);

I should work, right? but it doesn't!!!! it tells me that dbValue cannote be cast to RelationActiveEnum...


Say we have:

public enum MyEnum {
  Test1, Test2, Test3
}

To get the name of a enum variable use name():

MyEnum e = MyEnum.Test1;
String name = e.name(); // Returns "Test1"

To get the enum from a (string) name, use valueOf():

String name = "Test1";
MyEnum e = Enum.valueOf(MyEnum.class, name);

If you require integer values to match enum fields, extend the enum class:

public enum MyEnum {
  Test1(1), Test2(2), Test3(3);

  public final int value;

  MyEnum(final int value) {
     this.value = value;
  }
}

Now you can use:

MyEnum e = MyEnum.Test1;
int value = e.value; // = 1

And lookup the enum using the integer value:

MyEnum getValue(int value) {
  for(MyEnum e: MyEnum.values()) {
    if(e.value == value) {
      return e;
    }
  }
  return null;// not found
}

Since your 'value' also happens to match with ordinals you could just do:

public enum RelationActiveEnum {
    Invited,
    Active,
    Suspended;

    private final int value;

    private RelationActiveEnum() {
        this.value = ordinal();
    }
}

And getting a enum from the value:

int value = 1;
RelationActiveEnum enumInstance = RelationActiveEnum.values()[value];

I guess an static method would be a good place to put this:

public enum RelationActiveEnum {
     public static RelationActiveEnum fromValue(int value) 
             throws IllegalArgumentException {
         try {
              return RelationActiveEnum.values()[value]
         } catch(ArrayIndexOutOfBoundsException e) {
              throw new IllegalArgumentException("Unknown enum value :"+ value);
         }
     }
}   

Obviously this all falls apart if your 'value' isn't the same value as the enum ordinal.


You could create a lookup method. Not the most efficient (depending on the enum's size) but it works.

public static String getNameByCode(int code){
  for(RelationActiveEnum e : RelationActiveEnum.values()){
    if(code == e.value) return e.name();
  }
  return null;
}

And call it like this:

RelationActiveEnum.getNameByCode(3);

What you can do is

RelationActiveEnum ae = Enum.valueOf(RelationActiveEnum.class,
                                     RelationActiveEnum.ACTIVE.name();

or

RelationActiveEnum ae = RelationActiveEnum.valueOf(
                                     RelationActiveEnum.ACTIVE.name();

or

// not recommended as the ordinal might not match the value
RelationActiveEnum ae = RelationActiveEnum.values()[
                                     RelationActiveEnum.ACTIVE.value];

By if you want to lookup by a field of an enum you need to construct a collection such as a List, an array or a Map.

public enum RelationActiveEnum {
    Invited(0),
    Active(1),
    Suspended(2);

    private final int code;

    private RelationActiveEnum(final int code) {
        this.code = code;
    }

    private static final Map<Integer, RelationActiveEnum> BY_CODE_MAP = new LinkedHashMap<>();
    static {
        for (RelationActiveEnum rae : RelationActiveEnum.values()) {
            BY_CODE_MAP.put(rae.code, rae);
        }
    }

    public static RelationActiveEnum forCode(int code) {
        return BY_CODE_MAP.get(code);
    }
}

allows you to write

String name = RelationActiveEnum.forCode(RelationActiveEnum.ACTIVE.code).name();