If I have two variables:
Object obj;
String methodName = \"getName\";
Without knowing the class of obj
, how can I call the met
Use method invocation from reflection:
Class> c = Class.forName("class name");
Method method = c.getDeclaredMethod("method name", parameterTypes);
method.invoke(objectToInvokeOn, params);
Where:
"class name"
is the name of the classobjectToInvokeOn
is of type Object and is the object you want to invoke the method on"method name"
is the name of the method you want to callparameterTypes
is of type Class[]
and declares the parameters the method takesparams
is of type Object[]
and declares the parameters to be passed to the method