aspose file tools*
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 Spring in Action this week in the Spring 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get static value from interface at runtime