You can use reflection
sample class
package com.google.util;
class Maths {
public Integer doubleIt(Integer a) {
return a*2;
}
}
and use something like this-
step 1:- Load class
with given input name as String
Class<?> obj = Class.forName("Complete_ClassName_including_package");
//like:- Class obj = Class.forName("com.google.util.Maths");
step 2:- get Method
with given name and parameter type
Method method = obj.getMethod("NameOfMthodToInvoke", arguments);
//arguments need to be like- `java.lang.Integer.class`
//like:- Method method= obj.getMethod("doubleIt", java.lang.Integer.class);
step 3:- invoke
Method
by passing instance of Object and argument
Object obj2 = method.invoke(obj.newInstance(), id);
//like :- method.invoke(obj.newInstance(), 45);
YOU CAN DO STEP 2 LIKE THIS ALSO
(when you do not know particular method exists in a class
you check all method by looping method's array)
Method[] methods = obj.getMethods();
Method method = null;
for(int i=0; i < methods.length(); i++) {
if(method[1].getName().equals("methodNameWeAreExpecting")) {
method = method[i];
}
}