Class RecordDeclaration

All Implemented Interfaces:
NodeWithAccessModifiers<RecordDeclaration>, NodeWithFinalModifier<RecordDeclaration>, NodeWithPrivateModifier<RecordDeclaration>, NodeWithProtectedModifier<RecordDeclaration>, NodeWithPublicModifier<RecordDeclaration>, NodeWithStaticModifier<RecordDeclaration>, NodeWithStrictfpModifier<RecordDeclaration>, NodeWithAnnotations<RecordDeclaration>, NodeWithImplements<RecordDeclaration>, NodeWithJavadoc<RecordDeclaration>, NodeWithMembers<RecordDeclaration>, NodeWithModifiers<RecordDeclaration>, NodeWithParameters<RecordDeclaration>, NodeWithRange<Node>, NodeWithSimpleName<RecordDeclaration>, NodeWithTokenRange<Node>, NodeWithTypeParameters<RecordDeclaration>, Observable, Visitable, HasParentNode<Node>, Resolvable<ResolvedReferenceTypeDeclaration>, Cloneable

The record declaration

WARNING: This implementation is subject to change.

Java 1.0 to 13

Not available.

Java 14 (preview), Java 15 (2nd preview), Java 16

A definition of a record.
record X(...) { ... }

Note that the syntax of records is substantively different to standard classes/interfaces/enums. Specifically, note that record header contains the component declarations - where a "component" is defined as a non-static field.

This is in contrast to "normal" classes, where fields declarations are within the class body (optionally then initialised within a constructor.

Also note that the constructor for records does not accept any parameters.

Consider this example from https://openjdk.java.net/jeps/359


     record Range(int lo, int hi) {
         public Range {
             if (lo > hi)
                 throw new IllegalArgumentException(String.format("(%d,%d)",lo,hi));
         }
     }
 

To access these non-static field declarations, use getParameters()

Since:
3.22.0
Author:
Roger Howell
See Also:
https://openjdk.java.net/jeps/395, JLS 8.10 - Record Classes