• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Reflection: getting the value of public static final fields

 
Bear Bibeault
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Forget Steve. Look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic