JEP 492: Flexible Constructor Bodies (Third Preview)
PreviousJEP 491: Synchronize Virtual Threads without PinningNextJEP 494: Module Import Declarations (Second Preview)
Last updated
Last updated
Traditionally, Java constructors have required explicit placement of super()
or this()
as the first line in the constructor.
JEP 492 relaxes this rule:
β‘οΈ Now, you can run code before calling super()
or this()
β as long as you donβt access this
or instance members before initialization.
Although the Java language , it is considered bad practice: Item 19 of advises that "Constructors must not invoke overridable methods." To see why it is considered bad practice, consider the following class hierarchy: