Record Class Basics
A record class in Java is a special-purpose class that simplifies declaration and handling of an aggregate of values that comprise the state of a plain data object. A record class defines immutable fields and the compiler generates the get methods (also called getter or accessor methods) necessary to access the values of fields in instances of the record class. A record class generally does not provide any heavy processing of the data; the primarily goal is to allow users to access the data.
The basic syntax for a record class is shown below. The record header of a top-level record class can specify the public access modifier, as in a top-level normal class. The contextual keyword record is used in the header to declare a record class. It has this special meaning only in this context. A record class has a name, similar to a normal class. The header specifies a comma-separated component list that comprises field declarations (called record components), where each field declaration specifies the name and the type of the field. For the basic record class declaration, the record body can be left empty.
access_modifier
record
record_name
(
component_list
) // Record header
{ } // Empty record body
The CD_v0 class in Example 5.27 can be declared as a record class, as shown in Example 5.28. The public record class CD declared at (1) in Example 5.28 is equivalent to the normal class CD_v1 in Example 5.27. The compiler automatically generates the necessary fields, contructor, and method declarations for the record class CD. The client class DataUser in Example 5.28 utilizes data objects of record class CD.
Example 5.28 The CD Record Class
package record.basics;
import java.time.Year;
/** A record class that represents a CD. */
public record CD(String artist, String title, int noOfTracks, // (1)
Year year, Genre genre) { /* Empty body */ }
package record.basics;
import java.time.Year;
public class DataUser {
public static void main(String[] args) {
// Some ready-made CDs: (2)
CD cd0 = new CD(“Jaav”, “Java Jive”, 8, Year.of(2017), Genre.POP);
CD cd1 = new CD(“Jaav”, “Java Jam”, 6, Year.of(2017), Genre.JAZZ);
CD cd2 = new CD(“Funkies”, “Lambda Dancing”, 10, Year.of(2018), Genre.POP);
CD cd3 = new CD(“Genericos”, “Keep on Erasing”, 8, Year.of(2018), Genre.JAZZ);
CD cd4 = new CD(“Genericos”, “Hot Generics”, 10, Year.of(2018), Genre.JAZZ);
// An array of CDs.
CD[] cdArray = {cd0, cd1, cd2, cd3, cd4};
System.out.println(” Artist Title No. Year Genre”);
for(int i = 0; i < cdArray.length; ++i) {
CD cd = cdArray[i];
String cdToString = String.format(“%-10s%-16s%-4d%-5s%-5s”,
cd.artist(), cd.title(), cd.noOfTracks(), // (3)
cd.year(), cd.genre());
System.out.printf(“cd%d: %s%n”, i, cdToString);
}
System.out.println();
System.out.println(cd0.toString()); // (4)
CD cdX = new CD(“Jaav”, “Java Jive”, 8, Year.of(2017), Genre.POP);
System.out.println(“cd0.equals(cdX): ” + cd0.equals(cdX)); // (5)
}
}
Output from the program:
Click here to view code image
Artist Title No. Year Genre
cd0: Jaav Java Jive 8 2017 POP
cd1: Jaav Java Jam 6 2017 JAZZ
cd2: Funkies Lambda Dancing 10 2018 POP
cd3: Genericos Keep on Erasing 8 2018 JAZZ
cd4: Genericos Hot Generics 10 2018 JAZZ
cd0: CD[artist=Jaav, title=Java Jive, noOfTracks=8, year=2017, genre=POP]
cd0.equals(cdX): true
Leave a Reply