0

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.

Jack
  • 197
  • 1
  • 2
  • 11
  • Please read [mcve]. Give the exact error message and tell us in which line the error shows up. And just for the record: `class` is a **bad** choice for a name. – GhostCat Jun 29 '17 at 11:58
  • 1
    The name `class` for a variable is not just a bad choice; it's not allowed. The code you posted does not compile. – Jesper Jun 29 '17 at 13:00

0 Answers0