Java使用反射调用静态方法
我想调用main静态的方法。我得到了类型的对象Class,但是我无法创建该类的实例,也无法调用该static方法main。
回答:
// String.class here is the parameter type, that might not be the case with youMethod method = clazz.getMethod("methodName", String.class);
Object o = method.invoke(null, "whatever");
如果方法是私有使用getDeclaredMethod()而不是getMethod()。并调用setAccessible(true)方法对象。
以上是 Java使用反射调用静态方法 的全部内容, 来源链接: utcz.com/qa/424612.html
