Skip to content
Advertisement

With the new Java 14 Record functionality, is it possible of creating multiples constructors for the same Record?

I have a bunch of “data” classes using Lombok and I want to migrate all of them to use the new Record functionality available in Java 14. With the new Java 14 Record functionality, is it possible of creating multiples constructors for the same Record? If not, is there an alternative?

Advertisement

Answer

With Java 14, records couldn’t have multiple constructors (reference: Java 14 – JEP 359: Records (Preview)).

As of Java 15 and 16+, records may have multiple constructors. (see Java 15 – JEP 384: Records (Second Preview) and Java 16 – JEP 395: Records (Final)).

However, every constructor must delegate to the record’s canonical constructor which can be explicitly defined or auto-generated.

An example:

JavaScript

Another example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement