JEP 447 已发布,可在构造函数的 super()调用之前执行语句
![](/theme/default/default/images/main/eye-open.png)
我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。
![](/theme/default/default/images/main/icon-jing.png)
文章摘要:
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.
想要了解更多内容?
![](/theme/default/default/images/main/icon-jing.png)
![](https://cdn.easycorp.cn/rongpm/upload/202312/f_39217d624bb2b42ce8f6322ebd7e573a.png)
![](https://cdn.easycorp.cn/rongpm/upload/202312/f_39217d624bb2b42ce8f6322ebd7e573a.png)