Matheus, Danny,
While the code is nice and compact, in this specific case, I think doing something more like:
enum Side {
LEFT, TOP, RIGHT, BOTTOM;
public Side left() {
return toLeft;
}
public Side right() {
return toRight;
}
public Side opposite() {
return oppositeOf;
}
private Side toLeft, toRight, oppositeOf;
private void setSides(Side leftSide, Side rightSide, Side oppositeSide) {
toLeft = leftSide;
toRight = rightSide;
oppositeOf = oppositeSide;
}
static {
LEFT.setSides(BOTTOM,TOP,RIGHT);
RIGHT.setSides(TOP, BOTTOM,LEFT);
TOP.setSides(LEFT,RIGHT,BOTTOM);
BOTTOM.setSides(RIGHT,LEFT,TOP);
}
}
is simpler in that you don’t have to care about the order the Enum values are defined in and you don’t have to do calculations, array lookups, or retrieving ordinal positions to get what can be defined simply once and for all. The downside is storing 3 enum values for each enum which in this case is nothing. If the Enum had a lot more values (N,NNE,NE,ENE,E, etc…) or had a lot more relationships to track then the more compact code would look more attractive.
[quote]I’ve never been a fan of the static code chunk, if only because it’s hard to tell when it gets executed. For this, I figure it’s not too much of a problem, but I tend to not suggest it. xD
[/quote]
I got the static initializer idea from a stack overflow post. I was originally thinking along your lines but what I like about this is that the definition is complete within itself. You don’t have to remember to call a method from some where else. Then again, I tend to use statics more often than other Java programmers I’ve worked with.