import java.lang.reflect.*;
public class ReflectionExample {
public static void main(String args[])
throws
ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException,
InvocationTargetException
{
ClassLoader j = ClassLoader.getSystemClassLoader();
Class someClass = j.loadClass("GlobalVillage");
Object instanceOfSomething = someClass.newInstance();
// the second parameter specifies the type of the argument(s) passed to method
Method aMethod = someClass.getMethod("showMessage", new Class[]{String.class});
//Here value of the argument of the method is provided
Object returnValue = aMethod.invoke(instanceOfSomething, "The whole world is like a family.");
System.out.println("Show message says : " + returnValue);
}
}
class GlobalVillage {
public String showMessage(String strMessage) {
return strMessage; // isn't this true??
}
}
No comments