//PrivateMethod.java
class PrivateMethod
{
private void printMessage()
{
System.out.println("hello rakesh:");
}
}
//AccessPrivate.java
import java.lang.reflect.*;
class AccessPrivate
{
public static void main(String args[]) throws Exception
{
Class c=Class.forName("PrivateMethod");
Object o=c.newInstance();
Method m=c.getDeclaredMethod("printMessage",null);
m.setAccessible(true);
m.invoke(o,null);
}
}
答案 0 :(得分:0)
试试这段代码(我在cmd的jdk 1.7上测试过它):
Class<?> c=Class.forName("PrivateMethod");
Object o=c.newInstance();
Method m=c.getDeclaredMethod("printMessage");
m.setAccessible(true);
m.invoke(o);