What is benefit from using fromValue function instead of valueOf, java enums?

Bartek picture Bartek · Sep 4, 2017 · Viewed 8.4k times · Source

I see some programmers use in enums structure function called fromValue. What is it purpose, if we can use valueOf? For example, I found something like this:

public static FooEnum fromValue(String v) {  
    return valueOf(v);  
}

Answer

Marco Kunis picture Marco Kunis · Sep 4, 2017

Think about an enum that has a string property with a different value then the name of the enum.

public enum FooEnum {
   A("foo"),
   B("bar"),
   ;
  ...
 }

Then you need such an method for a lookup of the property values. See also this answer