扫码阅读
手机扫码阅读

JEP 447 已发布,可在构造函数的 super()调用之前执行语句

41 2024-07-04

文章摘要:

Java Enhancement Proposal (JEP) 447, titled "super() 前置语句 (预览)" and part of Project Amber, has been integrated into JDK 22 after review. Proposed by Oracle technical advisor Gavin Bierman and open for community feedback, JEP 447 allows statements that do not reference the instance being created to appear before the super() call in constructors, without compromising safety and initialization guarantees.

Traditionally, Java constructors have been required to start with an explicit call to another constructor, ensuring a top-down execution order and preventing access to uninitialized fields. However, this has significantly limited the expressiveness and readability of constructor logic. JEP 447 relaxes this constraint, providing an example where the constructor of SubClass includes logic to handle Certificate objects directly, enhancing readability and reducing the need for auxiliary methods.

JEP 447 maintains the fundamental guarantees of constructor behavior, ensuring that subclass constructors do not interfere with superclass instantiation. This update requires no modifications to the Java Virtual Machine (JVM), relying solely on the JVM's existing capabilities to verify and execute pre-constructor call code.

As Java continues to evolve, JEP 447 demonstrates Java's adaptation to modern programming practices, balancing the introduction of new features with the robustness of the Java ecosystem. For Java developers, it represents an opportunity to explore more efficient coding practices while adhering to the core principles of the programming language.

想要了解更多,点击 查看原文

为一线互联网公司核心技术人员提供优质内容。科技圈的观察者,前沿技术的传播者。

94 篇文章
浏览 3969
加入社区微信群
与行业大咖零距离交流学习
软件研发质量管理体系建设 白皮书上线