Forums Register Login

Getting value of static variable with Reflection ?

+Pie Number of slices to send: Send
Is it possible to get a value os a static final variable of a class with reflection ?
I can get the value of a instace variable normally starting from the a instance, since static fields are class variable
I wonder if it's possible.
+Pie Number of slices to send: Send
Yes. All the fields of a class (including static ones) are represented by an instance of the Field class.
+Pie Number of slices to send: Send
 

Joanne Neal wrote:Yes. All the fields of a class (including static ones) are represented by an instance of the Field class.


Could you please show me a simple example ?
+Pie Number of slices to send: Send
 

Vinicius Alves wrote:Could you please show me a simple example ?


Why don't you give it a try first. Have you even looked at the javadoc for the Field class ?
1
+Pie Number of slices to send: Send
Answering my topic:

+Pie Number of slices to send: Send
I assume you realise that will only work with String fields. You'll get a ClassCastException with any other type. If your method has to return a String, then you need to call toString on the Object returned from get().
+Pie Number of slices to send: Send
After checking if that return value is not null of course.
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 35248 times.
Similar Threads
java.lang.Void
How to get the list of static members and methods of a class?
logic behind not able to assign a static value to a non static variable
Is it possible?
can final variables be altered?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:45:36.