Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

lang.Record: practical reference

lang.Record: practical reference: Record is the implicit superclass of record classes, whose state is defined by components and generated value-oriented members.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.lang.Record

Record is the implicit superclass of record classes, whose state is defined by components and generated value-oriented members.

Use a record for a transparent, fixed set of components whose names and types define the state and generated accessors.

A record is final and cannot extend another class. Its component references are final, but referenced arrays and mutable objects are not made deeply immutable.

A compact canonical constructor may validate or normalize components. Generated equals compares array components by reference because it applies the component type equality rules.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        record Point(int x, int y) {}
        Record value = new Point(2, 3);
        System.out.println(value);
    }
}

Result

Point[x=2, y=3]

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources