On this page
  1. record
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

record

Prev: `String` | Up: builtins | Next: `sealed`
Rel: builtins, Classes, Records & Sealed Hierarchies
Ref: Java Docs — record
Tags: #public #prog #java #builtins #tier/core


Availability

Built-in / java.lang (Language Construct (Java 16+)) — available with zero import statements.

Syntax & Overview

public record Point(double x, double y) {}

Concise immutable data carrier that auto-generates final fields, canonical constructor, accessors, equals, hashCode, and toString.

Notes & Pitfalls

Example

record Point(int x, int y) {
    Point { if (x < 0 || y < 0) throw new IllegalArgumentException(); }
}
var p = new Point(3, 4);
assert p.x() == 3 && p.equals(new Point(3, 4));

Linked from

Related notes