java中void方法没有显式反馈任何是否成功调用的信息,我需要知道是否调用成功了,进行了尝试,下面的方法应该可以。 import java.lang.reflect.Method; public class VoidToNull { public void voidFunc(int testInt){ System.out.println("voidFunc("+testInt+")--print by itself"); } public static void main(String[] args){ try { VoidToNull v2n=new VoidToNull(); /* * 正常调用:v2n.voidFunc(6); --调用成功无返回值 * 反射调用:如下; --调用成功返回null */ Method method=VoidToNull.class.getMethod("voidFunc", new Class[]{int.class}); Object returnObj=method.invoke(v2n, new Object[]{6}); System.out.println("voidFunc(int)--reflect invoke return : "+returnObj); } catch (Exception e) { e.printStackTrace(); } } } 从上面的例子还可以引申出下面的用法,在参数位置调用void方法: public class ReflectCallVoidFunc { private String property; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public void printRCVF(ReflectCallVoidFunc rcvf){ System.out.println("printRCVF()--property : " + rcvf.getProperty()); } public static void main(String[] args) throws Exception{ ReflectCallVoidFunc rcvf=new ReflectCallVoidFunc(); //第一次 rcvf 的 property 为 null rcvf.printRCVF(rcvf); //第二次 rcvf 的 property 会被设置值 rcvf.printRCVF((rcvf.getClass().getMethod("setProperty", new Class[]{String.class}).invoke(rcvf, new Object[]{"hhl_program"}))==null?rcvf:rcvf); } }