简体   繁体   中英

Invoke enum in Java

Suppose I have a number of fields ( String or int ) in my Java enum, and I want to get a field value by its name , dynamically.

public enum Code {

    FIRST("valueForFirst"),
    SECOND("valueForSecond");
    // etc
}

Then I get the name of the field which I want:

String fieldName = getEnumFieldName(); // can be: "FIRST" or "SECOND" 
// now get "fieldName"'s value from Code

How can I do this ?

You need to use Enum.valueOf(); such as:

Code c = Code.valueOf(Code.class, fieldName);

If you are getting the name of your field from somewhere else in string form, you can use valueOf() method to get Enum instance.. But, first you would need to convert the string in all uppercase..

String fieldName = getEnumFieldName();
Code first = Code.valueOf(fieldName);
String value = first.getValue();

Go through this tutorial - http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more information on how to use Enums ..

You can define your enum like this :

public enum Code {

    private String value;

    public Code(String value) {
       this.value = value;
    }

    public String getValue() {
       return value;
    }

    FIRST("valueForFirst"),
    SECOND("valueForSecond");
}

and then use it like this :

Code code = Code.FIRST;
String val = code.getValue();

or like this :

String key = "FIRST";
Code code = Code.valueOf(key);
String val = code.getValue();

If you want to get "FIRST" from a Code, just do

String name = code.name();

You can use the valueOf() method on the enum.

String fieldName = "FIRST"; // or "SECOND" 
Code c = Code.valueOf(fieldName);

Here is the pattern which I use:

enum X {
    A("a"), B("b"), ...;

    private final static Map<String,X> MAP = new HashMap<String,X>();
    static {
        for( X elem: X.values() ) {
            if( null != MAP.put( elem.getValue(), elem ) ) {
                throw new IllegalArgumentException( "Duplicate value " + elem.getValue() );
            }
        }
    }

    private final String value;

    private X(String value) { this.value = value; }
    public String getValue() { return value; }

    // You may want to throw an error here if the map doesn't contain the key
    public static X byValue( String value ) { return MAP.get( value ); } 
}

It looks a bit odd to access the instances of the enum type in a static block inside of the enum declaration but this code works.

In your case, the could would look like so:

String fieldName = Code.valueOf(Code.class).getValue();
    public enum Code {

        FIRST("valueForFirst"),
        SECOND("valueForSecond");

    }

    public class Test{

       Code c;

       public static void main(String[] args){

        Test t = new Test();

        String val = t.c.FIRST.getValue();



        }

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM