Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get static value from interface at runtime

 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic