Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Java records in depth

Java records in depth: canonical and compact constructors, validation, custom accessors, static members, nesting, generics and what records cannot do.

A record is a compact way to declare an immutable data carrier. From a header listing its components, the compiler generates a private final field per component, a canonical constructor, an accessor method per component (named like the component, without get), and implementations of equals, hashCode and toString based on all components. Records are final, implicitly extend java.lang.Record, and became a standard feature in Java 16.

Use a record when a type is defined entirely by its data and two instances with equal components are interchangeable: coordinates, money amounts, query results, map keys, DTOs.

Java
record Point(int x, int y) { }

var p = new Point(3, 4);
p.x();              // 3
p.equals(new Point(3, 4));   // true

Try it

Java Compact constructor, validation and extra members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Accessors, defensive copies and generics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What you get and what you can add

Member Generated? Can you declare it yourself?
private final field per component Yes No other instance fields are allowed
Canonical constructor Yes Yes, explicit or compact form
Accessor name() Yes Yes; must be public, same return type, no throws
equals, hashCode, toString Yes Yes
Other constructors No Yes, must call another constructor with this(...)
static fields, methods, nested types No Yes
Instance methods, interfaces to implement No Yes

Usage notes

  • Shallow immutability. The fields are final, but a component holding a mutable object (List, array, Date) can still change. Copy defensively in the compact constructor.
  • Compact constructor: write no parameter list; assign to the parameters (not this.x = x) to normalize values. The fields are assigned automatically at the end.
  • Generated equals compares components with == for primitives (floating-point components are compared like Double.compare) and Objects.equals for references. Array components compare by identity, so avoid arrays.
  • Cannot extend a class, and cannot be extended (it is final), but it can implement interfaces, including sealed ones (sealed classes).
  • Annotations on components propagate to the field, accessor and constructor parameter depending on their targets.
  • Serialization: records serialize via their canonical constructor, so validation runs on deserialization.
  • Not a JavaBean: accessors are name(), not getName(); some frameworks need configuration for that. Because frameworks that expect a no-argument constructor and setters cannot populate records, check that your library supports them.
  • Local records are handy inside methods (local classes and records).