JEP! 513: Flexible Constructor Bodies

https://openjdk.org/jeps/513arrow-up-right

There may be discussion what is the purpose of this JEP and why not just static factory methods like Effective Java suggested:

  • now there is no need for forced factory methods

    • scala and kotlin cover this functionality in their Companion Object

    • now technically we can have it in one palce if we want to

  • in next versions when there are native classes it can be helpful to have validation in record when creating PhoneNumber("maybe-correct-number")

RUN :

com.wlodar.jeeps.jep513flexibleconstructors.Jep513BeforeValidationDemo
com.wlodar.jeeps.jep513flexibleconstructors.Jep513ValidationDemo

Just a simple comparison how data can be transformed before passing to super constructor. Now vs Before

RUN:

com.wlodar.jeeps.jep513flexibleconstructors.Jep513ConstructorChainingWithPreparationDemo

demonstrates record creation through this constructor chaining

RUN:

com.wlodar.jeeps.jep513flexibleconstructors.Jep513StillIllegalInConstructorPrologueDemo

demonstrates illegal references to this

RUN :

Solves EffectiveJava issue 19 - don't call non final method in super constructor

Last updated