For a project, I'm making this mock computer with all the basic instructions, like LOD, LODI, ADD, STO, etc..., and they're enums, and I have to "lookup" the instruction without using if statements or switch statements. So I'm trying to use this method to return the enum type so I can just one (or two) lines of code to automatically look it up.
public enum Example {
ALPHA, BETA, GAMMA;
public static void main(String[] args) {
Example ex = Example.valueOf("ALPHA");
System.out.println(ex);
}
}
Could you solve your problem more cleanly by introducing a polymorphic method, like method eval() here: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
> For a project, I'm making this mock computer with all
> the basic instructions, like LOD, LODI, ADD, STO,
> etc..., and they're enums, and I have to "lookup" the
> instruction without using if statements or switch
> statements. So I'm trying to use this method to
> return the enum type so I can just one (or two) lines
> of code to automatically look it up.
I don't know if this helps, but here's an example on how to use that method. Note that I've shown two different ways of getting the value.
class Test {
public static void main(String[] args) {
Instruction instruction = Instruction.valueOf("ADD");
System.out.println(instruction);
instruction = Enum.valueOf(Instruction.class, "ADD");
System.out.println(instruction);
}
}
enum Instruction {
ADD, MOVE, JSR, JMP;
}