Is it possible to get an enum by searching on its name instead of value ? For example :
public enum MyEnum {
TOTO("some value", 1);
ZOZO("some other value", 2);
}
And what I want to do is a getter like :
public static MyEnum getByName(String str) {
[...]
}
So I can do :
MyEnum foo = MyEnum.getByName("TOTO");
MyEnum bar = MyEnum.getByName("ZOZO");
Answer
Each Java enum has method valueOf(String name)
which returns enum by name.
MyEnum foo = MyEnum.valueOf("TOTO");
MyEnum bar = MyEnum.valueOf("ZOZO");
No comments:
Post a Comment