JVM Knowledge

To ensure proper initialization of its record components, a record class does not implicitly declare a default constructor (§8.8.9). Instead, a record class has a canonical constructor, declared explicitly or implicitly, that initializes all the component fields of the record class.

There are two ways to explicitly declare a canonical constructor in a record declaration: by declaring a normal constructor with a suitable signature (§8.10.4.1) or by declaring a compact constructor (§8.10.4.2).

Given the signature of a normal constructor that qualifies as canonical, and the signature derived for a compact constructor, the rules of constructor signatures (§8.8.2) mean it is a compile-time error if a record declaration has both a normal constructor that qualifies as canonical and a compact constructor.

Either way, an explicitly declared canonical constructor must provide at least as much access as the record class, as follows:

  • If the record class is public, then the canonical constructor must be public; otherwise, a compile-time error occurs.
  • If the record class is protected, then the canonical constructor must be protected or public; otherwise, a compile-time error occurs.
  • If the record class has package access, then the canonical constructor must not be private; otherwise, a compile-time error occurs.
  • If the record class is private, then the canonical constructor may be declared with any accessibility.

An explicitly declared canonical constructor may be a fixed arity constructor or a variable arity constructor (§8.8.1).

If a canonical constructor is not explicitly declared in the declaration of a record class R, then a canonical constructor r is implicitly declared in R with the following properties:

  • The signature of r has no type parameters, and has formal parameters given by the derived formal parameter list of R, defined below.
  • r has the same access modifier as R, unless R lacks an access modifier, in which case r has package access.
  • r has no throws clause.
  • The body of r initializes each component field of the record class with the corresponding formal parameter of r, in the order that record components (corresponding to the component fields) appear in the record header.

The derived formal parameter list of a record class is formed by deriving a formal parameter from each record component in the record header, in order, as follows:

  • If the record component is not a variable arity record component, then the derived formal parameter has the same name and declared type as the record component.

    If the record component is a variable arity record component, then the derived formal parameter is a variable arity parameter (§8.4.1) with the same name and declared type as the record component.

  • The derived formal parameter is annotated with the annotations, if any, that appear on the record component and whose annotation interfaces are applicable in the formal parameter context, or in type contexts, or both (§9.7.4).

A record declaration may contain declarations of constructors that are not canonical constructors. The body of every non-canonical constructor in a record declaration must contain an alternate constructor invocation (§8.8.7.1), or a compile-time error occurs.

8.10.4.1. Normal Canonical Constructors

A (non-compact) constructor in the declaration of record class R is the canonical constructor of R if its signature is override-equivalent (§8.4.2) to the derived constructor signature of R.

The derived constructor signature of a record class R is a signature that consists of the name R, no type parameters, and the formal parameter types derived from the record header of R by taking the declared type of each record component in order.

As a canonical constructor has a signature that is override-equivalent to the derived constructor signature of the record class, there can be only one canonical constructor declared explicitly in the record class.

The declaration of a (non-compact) canonical constructor must satisfy all of the following conditions, or a compile-time error occurs:

  • Each formal parameter in the formal parameter list must have the same name and declared type as the corresponding record component.

    A formal parameter must be a variable arity parameter if and only if the corresponding record component is a variable arity record component.

  • The constructor must not be generic (§8.8.4).

  • The constructor must not have a throws clause.

  • The constructor body must not contain an explicit constructor invocation (§8.8.7.1).

  • All the other rules for constructor declarations in a normal class declaration must be satisfied (§8.8).

A consequence of these rules is that the annotations on a record component can differ from the annotations on the corresponding formal parameter of an explicitly declared canonical constructor. For example, the following record declaration is valid:

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@interface Foo {}
@interface Bar {}

record Person(@Foo String name) {
    Person(@Bar String name) {
        this.name = name;
    }
}

8.10.4.2. Compact Canonical Constructors

A compact constructor declaration is a succinct form of constructor declaration, only available in a record declaration. It declares the canonical constructor of a record class without requiring the record components of the class to be manually repeated as formal parameters of the constructor.

CompactConstructorDeclaration:
  {ConstructorModifier} SimpleTypeName ConstructorBody

The following productions from §8.8, §8.8.3, and §8.8.7 are shown here for convenience:

ConstructorModifier:
  (one of)
  Annotation public protected private

SimpleTypeName:
  TypeIdentifier

ConstructorBody:
  { [BlockStatements] ConstructorInvocation [BlockStatements] }
  { [BlockStatements] }

It is a compile-time error for a record declaration to have more than one compact constructor declaration.

The formal parameters of a compact constructor of a record class are implicitly declared. They are given by the derived formal parameter list of the record class (§8.10.4).

The compact constructor of a record class is a variable arity constructor (§8.8.1) if the record class has a variable arity record component.

The signature of a compact constructor declaration is equal to the derived constructor signature of the record class (§8.10.4.1).

The body of a compact constructor declaration must satisfy all of the following conditions, or a compile-time error occurs:

  • The body must not contain a return statement (§14.17).
  • The body must not contain an explicit constructor invocation (§8.8.7.1).
  • The body must not contain an assignment to a component field of the record class.
  • All the other rules for a constructor in a normal class declaration must be satisfied (§8.8), except for the requirement that the component fields of the record class must be definitely assigned and moreover not definitely unassigned at the end of the compact constructor (§8.3.1.2).

If a record declaration has a record component named c, then the simple name c in the body of a compact constructor denotes the implicit formal parameter named c, and not the component field named c.

After the last statement, if any, in the body of the compact constructor has completed normally (§14.1), all component fields of the record class are implicitly initialized to the values of the corresponding formal parameters. The component fields are initialized in the order that the corresponding record components are declared in the record header.

The intent of a compact constructor declaration is that only code to validate or normalize parameters needs to be given in the constructor body; the remaining initialization code is supplied by the compiler. For example, the following record class has a compact constructor that simplifies a rational number:

record Rational(int num, int denom) {
    private static int gcd(int a, int b) {
        if (b == 0) return Math.abs(a);
        else return gcd(b, a % b);
    }

    Rational {
        int gcd = gcd(num, denom);
        num    /= gcd;
        denom  /= gcd;
    }
}

The compact constructor Rational {...} behaves the same as this normal constructor:

Rational(int num, int denom) {
    int gcd = gcd(num, denom);
    num    /= gcd;
    denom  /= gcd;
    this.num   = num;
    this.denom = denom;
}