Getting value of public static final field/property of a class in Java via reflection
Solution 1
First retrieve the field property of the class, then you can retrieve the value. If you know the type you can use one of the get methods with null (for static fields only, in fact with a static field the argument passed to the get method is ignored entirely). Otherwise you can use getType and write an appropriate switch as below:
Field f = R.class.getField("_1st");
Class<?> t = f.getType();
if(t == int.class){
System.out.println(f.getInt(null));
}else if(t == double.class){
System.out.println(f.getDouble(null));
}...
Solution 2
R.class.getField("_1st").get(null);
Exception handling is left as an exercise for the reader.
Basically you get the field like any other via reflection, but when you call the get method you pass in a null since there is no instance to act on.
This works for all static fields, regardless of their being final. If the field is not public, you need to call setAccessible(true)
on it first, and of course the SecurityManager has to allow all of this.
Solution 3
I was following the same route (looking through the generated R class) and then I had this awful feeling it was probably a function in the Resources class. I was right.
Found this: Resources::getIdentifier
Thought it might save people some time. Although they say its discouraged in the docs, which is not too surprising.
Related videos on Youtube
Viet
Developer who is passionate about web, C++, design, classical music, art and tries mixing them together.
Updated on July 08, 2022Comments
-
Viet almost 2 years
Say I have a class:
public class R { public static final int _1st = 0x334455; }
How can I get the value of the "_1st" via reflection?
-
Matthieu about 9 years
R._1st
couldn't work? If you're talking about Android development, I think theR
class is always there... -
Sevastyan Savanyuk over 6 years@Matthieu I thought so too, until this day when I had to do this very same thing, but only with the
BR
class.
-
-
Viet about 14 yearsthanks. I tried but it didn't work. Exception is thrown at the operation f.getInt(null). I caught it but how come there's an exception?
-
M. Jessup about 14 yearsWhat kind of exception did you receive?
-
Viet about 14 yearsHi, the Exception e.getMessage() returns the field name, which is "_1st" and nothing else.
-
M. Jessup about 14 yearsBut what is the type of the exception? (i.e. NullPointerException, SecurityException, ...)
-
Viet about 14 yearsI got it. The class I needed was actually R.id. Thanks for your help!
-
Viet about 14 yearsI got it. The class I needed was actually R.id. Thanks for your help!
-
Matthieu about 9 yearsSo you inferred it was an Android question. Should have been indicated in the tags...
-
Matthew Read over 7 yearsIt's not an Android question, it's a Java reflection question that uses a particular example. Questions are tagged based on their topic.
-
Sevastyan Savanyuk over 6 yearsHow come the documentation never mentions that
getInt()
ignores the passed in argument? Spent hours on trying to get the instance of the class to pass there.