Return to site

โ˜• JAVA TRICK: Compact Record Constructors

ยท java

You know how to use records to model data carriers andย 

how to verify incoming data during construction,

but did you know๐Ÿ’ก that you don't have to list the parameters of all record constructors?

A record's ๐’„๐’‚๐’๐’๐’๐’Š๐’„๐’‚๐’ constructor has one argument per component

but in its ๐’„๐’๐’Ž๐’‘๐’‚๐’„๐’• form, you don't have to list them.

You can't assign fields, that happens in compiler-generated code after yours,

but you can reassign the parameters, which leads to the same result.

๐–๐ˆ๐“๐‡ ๐ญ๐ก๐ž ๐‚๐จ๐ฆ๐ฉ๐š๐œ๐ญ ๐‚๐จ๐ง๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐จ๐ซ

๐–๐ˆ๐“๐‡๐Ž๐”๐“ ๐ญ๐ก๐ž ๐‚๐จ๐ฆ๐ฉ๐š๐œ๐ญ ๐‚๐จ๐ง๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐จ๐ซ

All Posts
ร—

Almost doneโ€ฆ

We just sent you an email.ย Please click the link in the email to confirm your subscription!

OK