using enum method valueOf

I'm trying to use the valueOf(class, string) method to return a particular enum type. I understand the string name, but how do you access the class syntactically? The class with the enum in it is called CPU, so how would I do it? Thanks.
[245 byte] By [Eridania] at [2007-11-27 4:35:36]
# 1
Sorry, but I don't understand the question. Can explain what you want to do, and why you want to do it?Kaj
kajbja at 2007-7-12 9:45:42 > top of Java-index,Java Essentials,New To Java...
# 2

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.

Eridania at 2007-7-12 9:45:42 > top of Java-index,Java Essentials,New To Java...
# 3

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

Hippolytea at 2007-7-12 9:45:42 > top of Java-index,Java Essentials,New To Java...
# 4

> 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;

}

kajbja at 2007-7-12 9:45:42 > top of Java-index,Java Essentials,New To Java...
# 5
You can access the class object of any type using the .class operator, or by invoking getClass on an instance of it, depending on when you need to know the class.
georgemca at 2007-7-12 9:45:42 > top of Java-index,Java Essentials,New To Java...