How to call a private method from outside a java class

后端 未结 5 1364
北海茫月
北海茫月 2020-12-01 03:33

I have a Dummy class that has a private method called sayHello. I want to call sayHello from outside Dummy. I think it sh

5条回答
  •  抹茶落季
    2020-12-01 04:23

    If you want to pass any parameter to private function you can pass it as second, third..... arguments of invoke function. Following is sample code.

    Method meth = obj.getClass().getDeclaredMethod("getMyName", String.class);
    meth.setAccessible(true);
    String name = (String) meth.invoke(obj, "Green Goblin");
    

    Full example you can see Here

提交回复
热议问题