There have been several updates related to “Java Data Classes” (AKA “Records”) in recent months. As I briefly mentioned in the post “Updates on JavaFX, Valhalla, Data Classes, and Java’s Floating-Point,” Brian Goetz‘s “Data Classes for Java” “explores possible directions for data classes in the Java Language.” Sadly, despite significant discussion on this potential new Java feature on the OpenJDK mailing lists, this document also points out, “This is an exploratory document only and does not constitute a plan for any specific feature in any specific version of the Java Language.”
In mid-March, Goetz posted a message to the amber-spec-experts mailing list titled “Records — current status.” He states the intent of records in Java in that message: “Our goal all along has been to define records as being ‘just macros’ for a finer-grained set of features. Some of these are motivated by boilerplate; some are motivated by semantics (coupling semantics of API elements to state.) In general, records will get there first, and then ordinary classes will get the more general feature.”
There are several interesting points made in the “Records — current status” post, but I’ll focus on a few here that I found particularly interesting. In general, one can see in the stated early design decisions that general principles that are now more popular than perhaps they were when Java was created dominate the thinking related to records.
Under the section “Mutability and accessibility,” Goetz proposes that Java records provide
final fields that are “package (
protected for abstract records) by default,” but which would allow the developer to “explicitly opt out of (non-final)” as well as allow accessibility to be “be explicitly widened (public).” I love the idea of a Java construct having
final fields by default and having to explicitly choose to make them non-
final rather than the other way around that we’ve become used to in Java.
In the “Accessors” section, Goetz writes that the current thought is to have these accessor methods NOT use the JavaBeans convention of “get” and instead use the field’s name without “get” prefix. He writes, “The obvious choice is to expose read accessors automatically. (These will not be named getXxx; we are not burning the ill-advised Javabean naming conventions into the language, no matter how much people think it already is.) The obvious naming choice for these accessors is fieldName().” I like the idea of automatically generated read accessors following this simple naming convention (which I tend to use when I write builders). I also appreciated the emphasized reassurance that there is no conspiracy or effort to “burn the ill-advised JavaBean naming convention into the language.”
Goetz’s “Core methods” section talks about common methods such as
toString(), and so forth. Goetz writes that “Records will get equals, hashCode, and toString” and that “there’s a good argument for making equals/hashCode final.” He adds that while there’s no need to make
final method, the automatically generated read accessor methods could be made
Stephen Colebourne has contributed multiple posts to the mailing list discussion regarding records/data classes in Java. These include his insights from presenting on Amber (the project housing this effort along with other efforts such as LVTI and raw string literals) and a response to the previously mentioned original “Records — current status” message.
Other relatively recent mailing list messages regarding records in Java include Goetz’s “Records: construction and validation,” a discussion started by Remi Forax on “Record and annotation values,” and a thread started by Gunnar Morling called “Records — Using them as JPA entities and validating them with Bean Validation.”
Although Records/Data Classes are not yet associated with any particular Java release, it’s exciting to think about the possibilities they might bring to enable better, safer, and more readable Java code.