This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes get static value from interface at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "get static value from interface at runtime" Watch "get static value from interface at runtime" New topic
Author

get static value from interface at runtime

Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
I need to retrieve value of a final static string in an interface. I don't know the interface name till run time. I cannot load the interface because no class implements the interface. Would you give me some suggestion?
Thanks
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Despite its name, the Class class can represent interfaces as well as classes. So use
<pre>
Class cl = Class.forName(interfaceName);
Field fld = cl.getField(fieldName);
String value = fld.get(null).toString();
</pre>


"I'm not back." - Bill Harding, Twister
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Thanks for your reply. It works like a charm. I thought I need to have an empty constructor to load a class. Since interface does not contain any implementing method, I may need a class that implement this interface. After revisiting the API, it does clarify my confusion.
Thanks a lot Jim
Joey
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: get static value from interface at runtime