Skip to content

Instantly share code, notes, and snippets.

@dburger
Created January 10, 2010 19:33
public enum Foo {
BAR(DEFAULT_VALUE), BAZ(44);
private static final int DEFAULT_VALUE = 42;
private int value;
Foo(int value) {
this.value = value;
}
/*
Foo.java:3: illegal forward reference
BAR(DEFAULT_VALUE), BAZ(44);
^
1 error
*/
}
import java.util.HashMap;
import java.util.Map;
public enum Foo {
BAR(""), BAZ("");
private static final Map<String, Foo> fooMap = new HashMap<String, Foo>();
static {
for (Foo foo : values()) {
fooMap.put(foo.getAlias(), foo);
}
}
private String alias;
Foo(String alias) {
this.alias = alias;
}
public String getAlias() {
return alias;
}
public static Foo fromAlias(String alias) {
return fooMap.get(alias);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment