Java 22 introduces JEP 447 as a preview feature, allowing statements before explicit constructor invocations (super(...) or this(...)) in constructors. Previously, Java enforced that no statements could precede these calls to guarantee top-down construction and no-access-before-initialization. The new 'prologue' block enables argument validation, preparation, and splitting without resorting to auxiliary constructors or inner classes. For example, splitting a string argument once and passing parts to super(...) is now straightforward. The feature operates in a 'pre-construction context' similar to a static method scope, ensuring the initialization rules are still respected.

9m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ Intro▚ Constructor Chaining▚ No Statements Before Constructor Invocation▚ JDK Enhancement Proposal 447▚ Prologue Benefits

Sort: