record (Java)

A compact Java class for plain data, declared with its components in one line, for which the compiler generates the fields, accessors, equals, hashCode and toString.

In more detail

Each component becomes a private final field with an accessor of the same name, so a record is shallowly immutable. Records cannot extend another class and are meant for transparent data such as request and response models. Validation goes in a compact constructor, and records can still have methods that derive views of their data. Records have been standard since Java 16.

Examples

public record Point(int x, int y) { }
public record OrderSummary(String id, int itemCount, String status) { }

Related terms

Taught in

References