对类上的 newInstance 进行简单反射调用时出现 InstantiationException?

发布于 2024-12-12 11:03:17 字数 1068 浏览 1 评论 0原文

我有一个抽象类 A,即

public abstract class A {

    private final Object o;

    public A(Object o) {
        this.o = o;
    }

    public int a() {
        return 0;
    }

    public abstract int b();

}

我有一个子类 B,即

public class B extends A {

    public B(Object o) {
        super(o);
    }

    @Override
    public int a() {
        return 1;
    }

    @Override
    public int b() {
        return 2;
    }

}

我正在执行以下代码:

Constructor c = B.class.getDeclaredConstructor(Object.class);
B b = (B) c.newInstance(new Object());

并在调用 newInstance 时收到 InstantiationException,更具体地说:

java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

我不知道为什么收到异常。我查看了其他一些类似的问题,并看到了有关调用超级构造函数时使用最终变量或父类的抽象性质问题的问题,但我找不到关于为什么这种特殊情况会引发 InstantiationException 的明确答案。有什么想法吗?

I have an abstract class A, i.e.

public abstract class A {

    private final Object o;

    public A(Object o) {
        this.o = o;
    }

    public int a() {
        return 0;
    }

    public abstract int b();

}

I have a subclass B, i.e.

public class B extends A {

    public B(Object o) {
        super(o);
    }

    @Override
    public int a() {
        return 1;
    }

    @Override
    public int b() {
        return 2;
    }

}

I am executing the following piece of code:

Constructor c = B.class.getDeclaredConstructor(Object.class);
B b = (B) c.newInstance(new Object());

and getting an InstantiationException on the call to newInstance, more specifically:

java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

I don't know why I'm receiving the exception. I have looked at some other similar questions and seen things about the usage of final variables when calling the super constructor or problems with the abstract nature of the parent class, but I could not find a definitive answer to why this particular situation throws an InstantiationException. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-12-19 11:03:17

您确定 B 不是用 abstract 关键字定义的吗?如果我将该类声明为公共抽象类 B,我可以重现该错误。

Are you certain that B is not defined with the abstract keyword? I can reproduce the error if I declare the class as public abstract class B.

假装爱人 2024-12-19 11:03:17

newInstance() 方法实际上不接受任何参数——它只触发零参数构造函数。如果您的类没有零参数的构造函数,它将抛出 InstantiationException。

The newInstance() method actually doesn't take any args -- it only triggers the zero-arg constructor. It will throw InstantiationException if your class doesn't have a constructor with zero parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文