Mastering Java 14 Records: Compact Constructors, Limitations, and Practical Examples
This article explains Java 14’s preview feature records, showing how they replace verbose classes with concise data carriers, detailing automatically generated members, compact constructors for validation, inherent limitations, and new reflection methods, all illustrated with clear code examples.
What are Java records?
JDK 14 introduced records as a new kind of type declaration, a restricted form of class similar to an enum. Records are intended as pure data carriers, containing only final fields and automatically generated accessor methods, a canonical constructor, equals, hashCode, and toString.
Note that records are a preview feature; they may change or be removed in future JDK releases. To compile and run code that uses preview features, the appropriate command‑line flags must be supplied.
Replacing a regular class with a record
Example of a typical Java class with final fields, a constructor, and getter methods:
final class FunTester {
final String name;
final int age;
public FunTester(String name, int age) { this.name = name; this.age = age; }
public String name() { return name; }
public int age() { return age; }
}The same functionality can be expressed concisely as: record FunTester(String name, int age) { } Each component becomes a private final field.
A public accessor method is generated for each component, named after the component.
A canonical constructor is generated whose signature matches the component list.
equals() and hashCode() are automatically implemented based on component values.
toString() returns a string representation containing the record name and component values.
Compact constructors
When additional validation or processing is needed during object creation, a record can define a compact constructor. The compact constructor has no explicit parameter list; the components are implicitly available.
record FunTester(String name, int age) {
public FunTester {
if (age < 0) {
throw new IllegalArgumentException("年龄不能为负数");
}
Objects.requireNonNull(name, "名字不能为空");
}
}Restrictions of records
Records cannot extend any other class.
Instance fields other than the implicit component fields must be static; they cannot be final instance fields.
Records are implicitly final and cannot be abstract.
All record components are implicitly final.
Aside from these constraints, records behave like ordinary classes. The java.lang.Class API adds two record‑specific methods: RecordComponent[] getRecordComponents() – returns an array describing the components. boolean isRecord() – returns true if the class is a record (similar to isEnum()).
Demo code
public static void main(String[] args) {
FunTester fun = new FunTester("FunTester", 25);
boolean record = fun.getClass().isRecord();
System.out.println(record); // true
RecordComponent[] recordComponents = fun.getClass().getRecordComponents();
for (RecordComponent rc : recordComponents) {
System.out.println(rc.getName()); // name, age
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
