Pages

Tuesday, July 17, 2012

Java how to invoke a static method using a string value

You can invoke a static method as below

Let's say you need to call SimpleClass.simpleMethod(int x);

Method m = SimpleClass.class.getDeclaredMethod("simpleMethod", Integer.TYPE);
m.setAccessible(true); //if security settings allow this
Object o = m.invoke(null, 20); //use null if the method is static

No comments:

Post a Comment