Why do I get “object is not an instance of declaring class” when invoking a method using reflection?

喜你入骨 提交于 2019-11-29 05:26:06

When you invoke a method via reflection, you need to pass the object you are calling the method on as the first parameter to Method#invoke.

// equivalent to s1.testParam("", obj)
testParamMethod.invoke(s1, "", obj);
 testParamMethod.invoke("", obj); ///// here getting error

The first parameter to invoke must be the object to invoke it on:

 testParamMethod.invoke(s1, "", obj); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!