record
A syntax for concisely defining immutable (read-only) data classes (officially introduced in Java 16). The constructor, accessor methods, equals(), hashCode(), and toString() are all auto-generated.
Syntax
// Define a record. The components in parentheses become the fields.
record RecordName(Type field1, Type field2) {
// You can also define additional methods here.
}
// Use a compact constructor (omitting the parameter list) to add validation.
record Point(int x, int y) {
Point {
if (x < 0 || y < 0) throw new IllegalArgumentException("Coordinates must be 0 or greater.");
}
}
// Accessor methods share the same name as the component (no "get" prefix).
Point p = new Point(3, 4);
int x = p.x(); // Access the x field via x().
int y = p.y();
// A record is a final class and cannot be extended, but it can implement interfaces.
record Range(int start, int end) implements Comparable<Range> {
@Override
public int compareTo(Range other) { return Integer.compare(start, other.start); }
}
Auto-generated members
| Member | Description |
|---|---|
| Constructor | A canonical constructor that takes all components as arguments is generated automatically. |
| Accessor methods | A getter named after each component is generated automatically (e.g., name() instead of getName()). |
| equals() | Returns true when all components are equal. |
| hashCode() | Generates a hash code based on all components. |
| toString() | Returns a string in the format RecordName[field1=value1, field2=value2]. |
Sample code
// Define and use a simple record.
record Point(int x, int y) {}
Point p1 = new Point(3, 4);
Point p2 = new Point(3, 4);
System.out.println(p1.x()); // Prints "3".
System.out.println(p1.y()); // Prints "4".
System.out.println(p1); // Prints "Point[x=3, y=4]".
System.out.println(p1.equals(p2)); // Prints "true".
// Define a record with validation.
record Person(String name, int age) {
Person {
if (name == null || name.isBlank()) throw new IllegalArgumentException("name cannot be blank.");
if (age < 0) throw new IllegalArgumentException("age must be 0 or greater.");
}
// Define an additional method.
String greeting() {
return "Hello, I'm " + name + " (" + age + " years old).";
}
}
Person alice = new Person("Alice", 30);
System.out.println(alice.greeting()); // Prints "Hello, I'm Alice (30 years old).".
// Implement an interface.
record Temperature(double celsius) {
double toFahrenheit() { return celsius * 9 / 5 + 32; }
}
Temperature t = new Temperature(100.0);
System.out.println(t.toFahrenheit()); // Prints "212.0".
Overview
A record is a syntax for defining "data classes" whose sole purpose is to hold data. Traditionally, this required a large amount of boilerplate code (constructors, getters, equals(), and so on), but with records you can define everything in a single line.
All fields in a record are immutable (final), so the values of an instance cannot be changed after it is created. A record is implicitly a final class and cannot be extended, but it can implement interfaces.
For a comparison with enums, see enum. For the basics of class definitions, see class / new / Constructor / this.
If you find any errors or copyright issues, please contact us.