The main method, static method, and member method without parameters of the class are called using the class name. The member methods with parameters are called. The Code is as follows:
Package com.evan.exe rcise; import java. lang. reflect. method; public class MainReflect {public static void main (String [] args) throws Exception {String className = "com.evan.exe rcise. test "; Class clazz = Class. forName (className); Method mainMethod = clazz. getMethod ("main", String []. class); Method variableMethod = clazz. getMethod ("test"); Method staticMethod = clazz. getMethod ("staticTest"); Method paraMethod = clazz. getMethod ("parameterTest", String. class, int. class); // call the main method mainMethod through reflection. invoke (null, (Object) new String [] {"evan"}); // call the parameter-free Member method variableMethod through reflection. invoke (Class. forName (className ). newInstance (); // call the static method staticMethod through reflection. invoke (clazz. newInstance (); // call the parameter member method paraMethod through reflection. invoke (clazz. newInstance (), (Object) new String ("evan"), (Object) 520) ;}} class Test {public static void main (String [] args) {for (String arg: args) {System. out. println (arg) ;}} public void test () {System. out. println ("calling common methods");} public void staticTest () {System. out. println ("called static method");} public void parameterTest (String name, int I) {System. out. println (name + I );}}