I have the following class:
public final class ClassMap {
public static final Class HELLO = HelloActivity.class;
}
I want to be able to access HelloActivity.class
knowing the string "HELLO"
. I've tried the following:
Field classField = ClassMap.class.getField("HELLO");
But that returns a Field
object. How can I get a Class
object back?
Thanks!
Now that you have the Field
object representing the field, ask for the value, ie call classField.get(Object obj)
.
Since your field is static
, the obj
parameter will be ignored, and you should just give a null
value. Javadoc says so:
If the underlying field is a static field, the
obj
argument is ignored; it may be null.
So, do this:
Field classField = ClassMap.class.getField("HELLO");
Object value = classField.get(null);
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.