Say you have the following enumeration :
public enum Color { RED("R"), GREEN("G"), BLUE("B"); private String shortName; private Color(String shortName) { this.shortName = shortName; } public static Color getColorByName(String shortName) { for (Color color : Color.values()) { if (color.shortName.equals(shortName)) { return color; } } throw new IllegalArgumentException("Illegal color name: " + shortName); } }
Since enum is a special case where you cannot just override the valueOf function, what is a naming convention to get around this and implement valueOf (string name)?
getColorByName(String name) getValueOf(String name) permissiveValueOf(String name) customValueOf(String name) forName(String name) getEnum(String name) getColor(String name)
Later Edit: I see Bloch in Effective Java 2nd edition. suggests something in the getInstance () lines (chapter 1, paragraph 1). Just add another option.
source share