Sub topic 10
What Records Cannot Do
A record is a short way to write a class that only holds data. The previous chapters showed what records give you for free. This chapter covers the limits. Java sets these limits on purpose, so that a record always stays a plain, predictable data carrier.
The Four Main Rules
Here are the rules every record follows:
| Rule | What it means | Allowed instead |
|---|---|---|
| A record is final | No other class can extend it | Share code through interfaces |
| A record cannot extend a class | It already extends `java.lang.Record` | Implement one or more interfaces |
| No extra instance fields | All data must be listed in the header | Static fields are fine |
| Fields are private and final | A component cannot change after construction | Create a new record with the new value |
A component is one of the values listed in the record's header, such as x and y in record Point(int x, int y). An instance field is a variable that belongs to each object. A static field belongs to the type itself, so all objects share one copy.
Seeing the Rules in Code
The program below compiles and runs. It shows what a record is allowed to have. The comments show lines that the compiler would reject.
interface Describable {
String describe();
}
record Point(int x, int y) implements Describable {
// Allowed: a static field is shared, not per object.
static final Point ORIGIN = new Point(0, 0);
// Not allowed: an extra instance field.
// private int color; // error: field declaration must be static
// Allowed: methods that use the components.
public String describe() {
return "(" + x + ", " + y + ")";
}
}
// Not allowed: extending a record.
// class Point3D extends Point { } // error: cannot inherit from final Point
// Not allowed: a record extending a class.
// record Pixel(int x) extends Object { } // error: '{' expected
public class Main {
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println(p.describe()); // (3, 4)
System.out.println(Point.ORIGIN); // Point[x=0, y=0]
// Not allowed: changing a component.
// p.x = 10; // error: cannot assign a value to final variable x
Point moved = new Point(10, p.y());
System.out.println(moved); // Point[x=10, y=4]
}
}The last part shows the usual workaround. Because you cannot change p, you build a new Point that holds the new value.
Why These Limits Exist
Every rule protects the same promise: a record is exactly its components.
- If a record had hidden extra fields, the generated
equalsandtoStringwould ignore them. Two records could look equal but behave differently. - If a subclass could add fields, the same problem would appear through inheritance.
- If fields could change, a record used as a key in a
HashMapcould get lost after itshashCodechanged.
When to Use a Regular Class Instead
Pick a normal class when you need any of these:
- Data that must change over time, such as a bank account balance.
- A parent class to inherit from, such as a framework base class.
- Hidden internal state, such as a cache or a counter, that should not count toward equality.
Exercise
A teammate writes record User(String name) { private int loginCount; }. Will it compile? If not, give two ways to fix it.
Show answer
It will not compile, because a record cannot declare extra instance fields. One fix is to add the value to the header: record User(String name, int loginCount) {}, and create a new User when the count changes. The other fix applies if the count must change in place: use a regular class instead of a record.
Exercise
Can a record implement an interface? Can a record extend another record?
Show answer
A record can implement any number of interfaces. A record cannot extend another record or any class, because every record already extends java.lang.Record and is final.