简体   繁体   中英

Java's Enum.valueOf() problem

I'm trying to derive an enum value from a String, like:

Level level = Enum.valueOf(Level.class, "WARNING");

But all I get is compiler warnings like:

Test.java:8: <T>valueOf(java.lang.Class<T>,java.lang.String) in java.lang.Enum cannot be applied to (java.lang.Class<java.util.logging.Level>,java.lang.String)

I know in JDK versions prior to 1.5 this:

Level level = Level.valueOf("WARNING");

would work, but I'm using JDK 1.6. Could anyone provide a working example for this kind of problem?

Thanks.

java.util.logging.Level simply isn't an enum. Did you actually mean that Level class, or a different one?

The second snippet you posted wouldn't work either, but if you're really talking about the normal Level class, you can use:

Level level = Level.parse("WARNING");

Level.WARNING , as well as Level.INFO and other Loggin levels are no ENUM's.

They're static final variables in the Level.java class.

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