• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection: getting the value of public static final fields  RSS feed

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to find a means to obtain the value of public static final fields in a class.

For example, given:



I'm determining the value of the class constant ABC.

So far, I've come up with:



However, that method requires that an instance of the declaring class be instantiated, therefore requiring that the declaring class have a no-args constructor. (This would be moot if I had an instance of the declaring class lying around, but I do not.)

Given that the field in question is public, static and final, it occurs to me that the value should be able to be determined without the need to instatiate an instance of the class, but I haven't been able to discern a means to do that.

Anyone know if this is possible without resorting to uber-hackery?

Extra credit: what if the field is declared in an interface?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bear]: that method requires that an instance of the declaring class be instantiated

No, for a static field you're allowed to pass null for the instance.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bear]: Extra credit: what if the field is declared in an interface?

Null works here too.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J2SE API Specification
java.lang.reflect.Field.get(Object obj)

"If the underlying field is a static field, the obj argument is ignored; it may be null."
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I must've looked at those descriptions over a dozen times and just skipped right over that part.

Can I use the fact that I'm waiting on my new glasses (bifocals for the first time, no less!) as an excuse?
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate that I am repeating this, but null works for getting static methods too. I found that out last week, but I thought the Javadocs didn't have that visible to easily.

But it works, I used it to invoke a static method.



Of course, it needs to be in a try-catch statement.

Mark
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a FAQ item to demo some of reflection stuff.
This is the link:
http://bobcat.webappcabaret.net/javachina/faq/a_01.htm#misc_Q60
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!