最近遇到一个问题,java非静态的内部类,在调用到java.lang.Class.getConstructor(Class<?>...)这个方法时,抛出了异常NoSuchMethodException,这是为什么呢?
仔细看java.lang.Class.getConstructor(Class<?>...)这个方法的注释,如下:
/** * Returns a <code>Constructor</code> object that reflects the specified * public constructor of the class represented by this <code>Class</code> * object. The <code>parameterTypes</code> parameter is an array of * <code>Class</code> objects that identify the constructor's formal * parameter types, in declared order. * * If this <code>Class</code> object represents an inner class * declared in a non-static context, the formal parameter types * include the explicit enclosing instance as the first parameter. * * <p> The constructor to reflect is the public constructor of the class * represented by this <code>Class</code> object whose formal parameter * types match those specified by <code>parameterTypes</code>. * * @param parameterTypes the parameter array * @return the <code>Constructor</code> object of the public constructor that * matches the specified <code>parameterTypes</code> * @exception NoSuchMethodException if a matching method is not found. * @exception SecurityException */
这个方法的用途,是返回一个 Constructor
对象,它反映此 Class
对象所表示的类的指定公共构造方法。对于非静态上下文中声明的内部类,应该把外部包装类作为第一参数。也就是说虽然我们想要得到的是内部类的构造方法,但实际传入的应该是外部类的类型作为参数,否则会抛出了NoSuchMethodException异常。我们可以写代码测试一下。
示例一:传入空参数
package mytest; import java.lang.reflect.Constructor; public class ClassTest { public static void main(String[] args) throws SecurityException, NoSuchMethodException { Constructor<InnerClass> constructor = InnerClass.class.getConstructor(); System.out.println(constructor.getName()); } public class InnerClass { public String name; } }运行结果:
Exception in thread "main" java.lang.NoSuchMethodException: mytest.ClassTest$InnerClass.<init>() at java.lang.Class.getConstructor0(Class.java:2715) at java.lang.Class.getConstructor(Class.java:1659) at mytest.ClassTest.main(ClassTest.java:8)
相关推荐
throwsException.java 声明抛出异常 useMyException.java 使用自定义异常 第7章 示例描述:本章学习Java的输入和输出。 CopyFile.java 文件复制实例 dir.java 显示目录下的所有文件和目录 encrypt.java 文件...
java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 6、说出Servlet的生命周期,并说出Servlet和CGI的区别。 Servlet被服务器实例化后,容器运行其init方法,...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 9、说出Servlet的生命周期,并说出Servlet和CGI的区别。 Servlet被服务器实例化后,容器运行其init方法...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 32 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 32 47、sleep() 和 wait() 有什么区别? 33 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 29 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在...
在try块中可以抛出异常吗? 29 二. Jav线程部分 7 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和...
在try块中可以抛出异常吗? 29 二. Jav线程部分 7 46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、...
1、一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? ...... 7 2、Java 有没有 goto? .......................................................................................................
protected : 保护成员,该类内部和继承类中可以访问。 public : 公共成员,完全公开,没有访问限制。 internal: 在同一命名空间内可以访问。 2 .列举ASP.NET 页面之间传递值的几种方式。 答. 1.使用QueryString,...