//Loader.java // // A simple program to demonstrate dynamic loading and usage of a class. import java.lang.reflect.*; // Contains the Method class. public class Loader { public static void main(String [] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { String strClassName = "Talker"; // Name of the class to be loaded. String strMethodName = "say"; // Name of the method to be executed in the loaded class. Class [] parameterTypes = new Class[0]; // List of parameter types for the aformentioned method. In this case, // there are none. String [] arguments = new String[0]; // Actual values of the aformentioned arguments. Method methodBuff; // A method buffer used to deal with a method in the loaded class. Object returnValue; // An untyped return value; must be type-cast to be used. Object anInstance; // An untyped instance variable for the class to be loaded. Class classBuff; // A buffer for the class to be loaded. classBuff = Class.forName( strClassName ); // loads the class named by the given string. anInstance = classBuff.newInstance(); // creates an instance of the previously loaded class. methodBuff = classBuff.getMethod( strMethodName, parameterTypes ); // returns a method of given name and array of parameter types from the loaded class // for use. returnValue = methodBuff.invoke( anInstance, arguments ); // A shell for the method to invoke itself. 'arguments' is an array of values for // the arguments. } }