March 2019
Intermediate to advanced
242 pages
6h 21m
English
Let's redefine the Emp class, which we used at the beginning of the chapter, as a data class:
record Emp(String name, int age) { } // data class - one liner // code
The preceding code uses the record keyword to define a data class, accepting a comma-separated variable name and type, required to store the state. The compiler automatically generates default implements for the object methods (equals(), hashCode(), and toString()) for data classes.
The code looks clear and compact. A reader would immediately know the intent of this single line of code—a carrier of the data name (type String) and age (type int). Another advantage for a reader is that they wouldn't have to read through constructors, accessors, ...
Read now
Unlock full access