How to call private method from another class in java




import java.lang.reflect.Method;

public class
{
    private void message() 
    {
        System.out.println("hello java");
    }
}

public class MethodCall
{
    public static void main(String[] args) throws Exception 
    {
        Class c = Class.forName("A");
        Object o = c.newInstance();
        Method m = c.getDeclaredMethod("message", null);
        m.setAccessible(true);
        m.invoke(o, null);
    }
}



No comments:

Post a Comment