This is an example of
Java Reflection which enables you to discover at runtime the "content" of any class. The getMethod() method is invoked on a Class object and requires a method name and a list of arguments. The name and arguments univoquely make up the method signature. The first argument is the method name and the second a Class array containing the ordered types of the arguments, that is:
theSet = MyClass.class.getMethod("setVal",params);
theSet is a java.lang.reflect.Method object and represents the method in class MyClass having the following signature:
setVal(boolean b);
Now, setting params to a zero-length Class array, means that the next method you want to retrieve does not take any parameter, that is:
theGet = MyClass.class.getMethod("getVal",params);
theGet is a java.lang.reflect.Method object and represents the method in class MyClass having the following signature:
getVal();
The reason why we pass a zero-length array is because the Class.getMethod method requires a Class array to be passed. If that array does not contain any component, then it means that the method you want to retrieve does not have any paramaters.
Now that you have the Method objects you can invoke them. For instance,
MyClass m = new MyClass();
theGet.invoke(m,new Object[]{});
will invoke getVal() on the MyClass object m.
MyClass m = new MyClass();
theSet.invoke(m,new Object[]{new Boolean(true)});
will invoke setVal(true) on the MyClass object m.
[ April 03, 2002: Message edited by: Valentin Crettaz ]