This class could be a record

Saturday, 14 March 2026

This post is about the class can be record inspection, and the corresponding quick-fix, that is present in IntelliJ IDEA. I describe what it does and a few interesting things about it.

Intro

For brevity, I will omit imports in code snippets. If you want to try them yourself, add the following module import at the top:

import module java.base;

Records in Java

Records are a Java feature introduced in Java 16 (released March 2021, specified in JEP 395). A record is a transparent carrier for immutable data, similar to Kotlin’s data class.

Here’s a simple record:

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

The main benefit of using a record is that a concise declaration, like the one above, results in a class which:

Before records, we had to write much more code to express it:

class Point {
    private final int x;
    private final int y;
    private final int z;

    Point(int x, int y, int z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }

    int x() {
        return x;
    }

    int y() {
        return y;
    }

    int z() {
        return z;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point)o;
        return x == point.x && y == point.y && z == point.z;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y, z);
    }
}

Records have many restrictions, but they can have constructors and methods:

record Point(int x, int y, int z) {
    Point(int x) {
        this(x, 0, 0);
    }

    Point(int x, int y) {
        this(x, y, 0);
    }

    double distanceFromOrigin() {
        return Math.sqrt(x * x + y * y + z * z);
    }

    static Point origin() {
        return new Point(0, 0, 0);
    }
}

It’s also possible to declare the canonical constructor explicitly, too:

record Point(int x, int y, int z) {
    Point(int x, int y, int z) {
        this.x = Math.max(0, x);
        this.y = Math.max(0, y);
        this.z = Math.max(0, z);
    }
}

The inspection

To aid in adoption of this Java feature, IntelliJ IDEA provides the aforementioned class can be record inspection. It highlights classes that are “data carrier like” and could be converted to a record. It also has a quick-fix that performs the conversion automatically, saving the developer from repetitive and error-prone work.

Interesting behaviors

There are a couple of interesting cases that this inspection has to consider.

Users expect IntelliJ IDEA’s quick-fixes to never result in “red code” i.e. code that does not compile.

Even more importantly, they expect the quick-fix to preserve semantics of their code. Even if the quick fix changes semantics only a bit, and only in rare cases, at the scale of millions of IntelliJ IDEA users it will certainly result in some fatal bugs. Thus, correctness of quick-fixes is paramount.

Loosening access privileges

All instance fields of a record are always private final:

public record Point(int x, int y, int z) {}
// x, y, and z are private final

But the accessor methods for these fields are always public! Accessor methods have the same name as their corresponding instance fields:

Point p = new Point(1, 2, 3);
System.out.println("x: " + p.x() + ", y: " + p.y() + ", z: " + p.z());

This differs a little from the age-old Java convention of e.g. a method getX being a getter/an accessor for an instance field x.

Let’s say you want to convert to record a class that has different visibility from its instance fields getters, for example:

public class User {
    private final String name;
    private final List<String> roles;

    public User(String name, List<String> roles) {
        this.name = name.trim();
        this.roles = List.copyOf(roles);
    }

    String getName() {
        return name;
    }

    List<String> getRoles() {
        return roles;
    }
}

In such a case, IntelliJ IDEA will warn you (because record accessors are always public):

Automatic redirection of constructors

A normal Java class can have many constructors, and each of these constructors can either redirect to another constructor, or assign all instance fields directly.

public class User {
    public final String name;
    public final int age;
    public final List<String> roles;

    public User(String name) {
        this(name, 0);
    }

    public User(String name, int age) {
        this.name = name.trim();
        this.age = age;
        this.roles = List.of();
    }

    public User(String name, int age, List<String> roles) {
        this.name = name.trim();
        this.age = age;
        this.roles = List.copyOf(roles);
    }
}

Record constructors cannot assign to instance field directly (except the canonical constructor). As a result, you must use a redirecting constructor call. The quick-fix handles this well – the class User from above is converted to the following record:

public record User(String name, int age, List<String> roles) {
    public User(String name) {
        this(name, 0);
    }

    public User(String name, int age) {
        this(name.trim(), age, List.of());
    }

    public User(String name, int age, List<String> roles) {
        this.name = name.trim();
        this.age = age;
        this.roles = List.copyOf(roles);
    }
}

Canonical constructor “stealing” calls

Let’s consider the following class:

class User {
    private final String name;
    private final List<String> roles;

    User(String name, Collection<String> roles) {
        this.name = name.trim();
        this.roles = List.copyOf(roles);
    }

    User(String name) {
        this(name, List.of());
    }

    String getName() {
        return name;
    }

    List<String> getRoles() {
        return roles;
    }
}

You’d expect the above to be a no-brainer candidate for conversion to a record:

record User(String name, List<String> roles) {
    User(String name, Collection<String> roles) {
        this(name.trim(), List.copyOf(roles));
    }

    User(String name) {
        this(name, List.of());
    }
}

This is dangerous.

The call sites that passed a List (a subtype of Collection) to the User constructor now resolve to a different constructor!

Consider this code:

List<String> adminRoles = new ArrayList<>(List.of("banUser", "deleteUser"));
User user = new User(" alice ", adminRoles);

Before conversion to record, the User constructor call resolved to User(String, Collection<String>). After conversion it resolves to User(String, List<String>), i.e. the record canonical constructor. This canonical constructor does not call trim() on the string and does not perform a defensive copy of the list.

This could be a fatal mistake, and one that you’d be angry at your IDE for suggesting, understandably so.

This is why, for the class shown above, IntelliJ IDEA does not report that “class can be record”.

Canonical constructor breaking implicit overload resolution

Let’s consider the class below:

class WeirdPoint {
    private final int a;
    private final byte b;
    private final int c;

    WeirdPoint(byte a, int b, int c) {
        this.a = a;
        this.b = (byte) b;
        this.c = c;
    }
}

I suspect an alarm goes off in your head saying “this can be a record!”, like this:

record WeirdPoint(int a, byte b, int c) {
    WeirdPoint(byte a, int b, int c) {
        this((int) a, (byte) b, c);
    }
}

Again, such a conversion looks fine at first glance. Unfortunately, if you had a code like this in your program, it no longer compiles:

void main(String[] args) {
    byte a = 1;
    byte b = 2;
    int c = 3;
    WeirdPoint p = new WeirdPoint(a, b, c);
}
$ java Main.java
Main.java:5: error: reference to WeirdPoint is ambiguous
    WeirdPoint p = new WeirdPoint(a, b, c);
                   ^
  both constructor WeirdPoint(byte,int,int) in WeirdPoint and constructor WeirdPoint(int,byte,int) in WeirdPoint match

This is again due to how overload resolution works in Java.

Before the class-to-record conversion, the only constructor’s signature was WeirdPoint(byte, int, int). The call site passed a (byte, byte, int) triple, but it still was resolved. Why?

When checking whether arguments are applicable to a method call (like a constructor), Java allows widening primitive conversions like byte -> int. This is why it worked before the conversion.

After the conversion to record, there are now two constructors:

and the same call (byte, byte, int) fits both.

Java tries to find a unique most specific overload, but since none is more specific, it considers the call to be ambiguous.


Because of this, IntelliJ IDEA does not report “class can be record” in this case.

Summary

If you’d like to learn more such quirks of Java before you encounter them, I recommend Tagir Valeev’s book 100 Java mistakes and how to avoid them. I am fortunate that Tagir is the Tech Lead on the Java team I work on at JetBrains.

PS

These days, LLMs often make large refactorings much easier. That said, I believe there’s (still) a significant value in “traditional” refactoring tools, such as those provided by IntelliJ IDEA. They are free to run (unlike LLMs), deterministic (unlike LLMs), and if there’s a bug in their behavior, it can be investigated and fixed by engineers (unlike LLMs). IntelliJ’s inspections are also extensively tested – there are more than 150 test cases for the “class can be record”.

The approach I like: enable as many inspections, and strive for 0 warnings. You can tell an LLM to call the get_file_problems tool from IntelliJ MCP to ensure it.