In the database I'm using this with there are magic numbers which I want to map to the State
enum, and vice-versa. I'm intrigued by the static declaration of undefined.code = 0
. What does this declaration, if that's what it is, actually do?
package net.bounceme.dur.data;
public enum State {
undefined(0), x(1), o(2), c(3), a(4), l(5), d(6);
private int code = 0;
static {
undefined.code = 0;
x.code = 1;
o.code = 2;
c.code = 3;
a.code = 4;
l.code = 5;
d.code = 6;
}
State(int code) {
this.code = code;
}
public int getCode() {
return this.code;
}
public static State getState(int code) {
for (State state : State.values()) {
if (state.getCode() == code) {
return state;
}
}
return undefined;
}
}
Currently, the usage for this enum factory method is as so:
title.setState(State.getState(resultSet.getInt(5)));
but I would be interested in any and all alternatives.