advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 3/5 | Rate this item | 2 users have rated this item.
Expertise: Intermediate
Language: Java
November 28, 2005
Using Reflection to Execute Class Methods at Runtime
Suppose you have a class and want to use it at runtime. The code below shows how you can load the class and execute a desired method at runtime. The assumption here is that the method name's is setProperties and it takes an argument of type java.util.Properties.

Class newClass = null;
Method getInstanceMethod = null;
Properties prop = new Properties();
prop.put("argName", "argValue");
try
{
Object obj = newClass.newInstance();
getInstanceMethod = newClass.getMethod("setProperties",
new Class[]{new Properties().getClass()});
}
catch (Exception ex)
{
//Exception in loading Class:" + ex.getMessage();
}
MS Sridhar
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement