I'm very familiar with using Enums in other languages, but I'm having some difficulty in Java with a particular use.
The Sun documentation for Enums boldly states:
"Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers."
Well, that's dandy, but I kind of need to have a constant datatype representation for each of the Enums, for comparison reasons in a switch statement. The situation is as follows: I'm constructing nodes which will represent a given space, or 'slot' in a maze graph, and these nodes must be able to be constructed from a 2D integer array which represents the maze. Here's what I've got for the MazeNode class, which is currently where the problem is (the switch statement barks):
NOTE: I know this code does not function, due to the dynamic item in the case statement. It is there to illustrate what I'm after.
public class MazeNode
{
public enum SlotValue
{
empty(0),
start(1),
wall(2),
visited(3),
end(9);
private int m_representation;
SlotValue(int representation)
{
m_representation = representation;
}
public int getRepresentation()
{
return m_representation;
}
}
private SlotValue m_mazeNodeSlotValue;
public MazeNode(SlotValue s)
{
m_mazeNodeSlotValue = s;
}
public MazeNode(int s)
{
switch(s)
{
case SlotValue.empty.getRepresentation():
m_mazeNodeSlotValue = SlotValue.start;
break;
case SlotValue.end.getRepresentation():
m_mazeNodeSlotValue = SlotValue.end;
break;
}
}
public SlotValue getSlotValue()
{
return m_mazeNodeSlotValue;
}
}
So the code complains on the switch statement with "case expressions must be constant expressions" -- I can see why the compiler might have trouble, since technically they are dynamic, but I'm not sure what approach to take to resolve this. Is there a better way?
The bottom line is I need the Enum to have corresponding integer values for comparison against the incoming 2D array of integers in the program.