import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Test { /** * @param args */ public static void main(String[] args) { try { Class cls = com.lwf.util.CommonUtil.class; Object obj = cls.newInstance(); Method addMethod = cls.getMethod("add", new Class[]{int.class,int.class}); Object value = addMethod.invoke(obj, new Object[]{new Integer(12),new Integer(23)}); System.out.println((Integer)value); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } package com.lwf.util; public class CommonUtil { public static void getName(){ System.out.println("this is a getName function"); } public void getMethod(){ System.out.println("this is a getMethod funciton"); } public int add(int m ,int n){ return m + n; } }