Reputation: 197
I am new one for java. I am not able to understand the use of method.invoke. Please could you give me the more detail. Java Code:
import java.util.Comparator;
import java.lang.reflect.Method
public class TestComparator implements Comparator {
private Class classObject;
public TestComparator(Class classObject,String methodName){
this.classObject= classObject;
this.methodName = methodName;
}
public int compare(Object object1, Object object2) {
try{
Method method = classObject.getMethod(("get"+methodName));
String value1 = method.invoke(classObject.cast(object1)).toString();
String value2 = method.invoke(classObject.cast(object2)).toString();
return value2.compareTo(value1);
}catch(Exception ex){
// ex.printStackTrace();
}
return 0;
}}
Thanks in advance.
Upvotes: 0
Views: 73