扫码阅读
手机扫码阅读

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

102 2024-07-04

我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。

查看原文:JEP 447 已发布,可在构造函数的 super()调用之前执行语句
文章来源:
InfoQ
扫码关注公众号

文章摘要:

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.

想要了解更多内容?

查看原文:JEP 447 已发布,可在构造函数的 super()调用之前执行语句
文章来源:
InfoQ
扫码关注公众号

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

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