If I have two variables:
Object obj;
String methodName = "getName";
Without knowing the class of obj
, how can I call the method identified by methodName
on it?
The method being called has no parameters, and a String
return value. It's a getter for a Java bean.
Best Answer
Coding from the hip, it would be something like:
The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give
methodName
).Then you invoke that method by calling
Again, leave out the arguments in
.invoke
, if you don't have any. But yeah. Read about Java Reflection