Void对象不能被new?因为修饰符

文章讨论了Java中Void类的特性,重点在于其私有无参构造函数,不允许实例化。同时强调final类的性质,即不可被继承以保证代码实现的稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上结论:无参构造被修饰为private,也没有其它构造。所以不可以new对象

下图为Void源码

package java.lang;

/**
 * The {@code Void} class is an uninstantiable placeholder class to hold a
 * reference to the {@code Class} object representing the Java keyword
 * void.
 *
 * @author  unascribed
 * @since   JDK1.1
 */
public final
class Void {

    /**
     * The {@code Class} object representing the pseudo-type corresponding to
     * the keyword {@code void}.
     */
    @SuppressWarnings("unchecked")
    public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");

    /*
     * The Void class cannot be instantiated.
     */
    private Void() {}
}

 同时还有不可被继承,当一个类被声明为final时,它表示这个类是最终的,不能被任何子类所继承。这是为了确保final类中的实现是不可以被改变的,或者它们代表了完全不可扩展的类。如果你尝试继承一个final类,编译器将会报错,指出不能继承一个final类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值