Quick revision
The short answer
For record Point(int x, int y) {}, the compiler generates a final class extending java.lang.Record with private final fields x and y, a canonical constructor that takes both, accessor methods x() and y() rather than getX(), and equals(), hashCode() and toString() based on all components. Records cannot extend other classes, cannot declare instance fields beyond their components, and are implicitly final, but they can implement interfaces and declare static fields, static and instance methods, and nested types. Records became a standard feature in Java 16 through JEP 395.
What to remember
In this answer, we saw that record Point(int x, int y) {} becomes a final class extending java.lang.Record, with private final fields, a canonical constructor, accessors x() and y(), and equals(), hashCode() and toString() based on all components. Records cannot extend classes, cannot add instance fields and are implicitly final, because they must be exactly their components, but they can implement interfaces, declare static members, instance methods, nested types and a compact constructor for validation. Records became standard in Java 16 with JEP 395.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For record Point(int x, int y) {}, the compiler generates a final class extending java.lang.Record with private final fields x and y, a canonical constructor that takes both, accessor methods x() and y() rather than getX(), and equals(), hashCode() and toString() based on all components. Records cannot extend other classes, cannot declare instance fields beyond their components, and are implicitly final, but they can implement interfaces and declare static fields, static and instance methods, and nested types. Records became a standard feature in Java 16 through JEP 395.
Why this question is asked
Records are now the normal way to write data carriers in Java: DTOs, query results, events, keys in maps and small value types. They remove dozens of lines of boilerplate, but they also come with rules that surprise people, such as accessor names and the ban on extra fields. Interviewers ask what a record generates to check that we know exactly what we get for free and what we give up. In this answer, we will write out what the compiler generates for Point, test each generated member, go through the restrictions and the reasons behind them, and see what we are still allowed to add.
1A Quick Recap: What a Record Is For
A record is a class whose whole purpose is to carry a fixed set of values, called its components. The declaration record Point(int x, int y) says that a Point is exactly its x and y, nothing more. From that statement the compiler can generate everything a data class needs, because it knows the state completely. The design goal, stated in JEP 395, is that a record is a transparent carrier for immutable data.
2What the Compiler Generates
Here is roughly the class javac produces for the one-line declaration. We can confirm it with javap -p Point.class.
- final class Point extends java.lang.Recordcannot be subclassed, cannot extend anything else
- private final int x, yone field per component, nothing else
- Canonical constructor Point(int x, int y)can be customized, compact form for validation
- Accessors x() and y()named after the components
- equals, hashCode, toStringbased on all components
5 layers, from the top down. Step through them one at a time.
Every part follows from the component list. The fields are private and final, so the state cannot change after construction. The canonical constructor takes every component in order. The accessors are named exactly like the components, x() and not getX(), because a record describes its components, not JavaBean properties. The equals(), hashCode() and toString() methods are generated through a JDK bootstrap method, ObjectMethods, so they are compact in the bytecode.
3Testing the Generated Members
The generated methods behave like a careful hand-written value class.
Notice that equals() is value-based but == is still identity: a record is an ordinary object. Because equals() and hashCode() are consistent and the fields are final, records make safe keys for HashMap and HashSet, which is a classic source of bugs with hand-written classes. For components of type float or double, equals() compares them the way Float.compare and Double.compare do, so a NaN component equals another NaN.
4Restrictions and Why They Exist
The restrictions all protect the promise that a record is exactly its components. A record cannot extend another class, because it already extends java.lang.Record, and inherited state would be state that is not in the component list. It cannot declare instance fields beyond its components, for the same reason. It is implicitly final, so no subclass can add state or change equals(). It also cannot declare instance initializer blocks, and it cannot be abstract.
The last example shows the main customization point. A compact canonical constructor, written without a parameter list, runs before the fields are assigned and is the place to validate or normalize components. The fields are then assigned automatically from the (possibly reassigned) parameters. Note that final fields make the record shallowly immutable only: a List component can still be modified by whoever holds the list, so we copy it with List.copyOf() in the compact constructor when that matters.
5What Records Can Still Have
Records are not limited to data. They can implement interfaces, which makes them a natural fit for sealed hierarchies. They can declare static fields and static methods, such as constants and factory methods, instance methods that compute from the components, and nested types. A record declared inside another class is implicitly static, so it never captures an outer instance.
Records were previewed in Java 14 and 15 and became a standard feature in Java 16 through JEP 395, so every current LTS release, including Java 17, 21 and 25, supports them fully. Jackson, Hibernate 6 for embeddables and projections, and Spring's configuration properties binding all support records, which is why they have become the default for data carriers.
6Follow-up questions
Can we override the generated accessors or equals()?
Yes. We can declare an accessor explicitly, for example to return a defensive copy, as long as it has the same name, return type and is public, and we can override equals(), hashCode() and toString(). But the overrides must keep the record's contract: a record copied from its components must be equal to the original.
Can a record be a JPA entity?
No. JPA entities need a no-argument constructor and mutable state that the provider can set, and usually proxies through subclassing, all of which records forbid. Records work well as DTOs, query projections and, in Hibernate 6, as embeddable values.
Why are accessors called x() and not getX()?
Because a record's API is defined by its components. Naming the accessor exactly like the component keeps the declaration and the API identical, and records were never meant to be JavaBeans. Most frameworks, including Jackson and Spring, understand record accessors directly.
7Conclusion
In this answer, we saw that record Point(int x, int y) {} becomes a final class extending java.lang.Record, with private final fields, a canonical constructor, accessors x() and y(), and equals(), hashCode() and toString() based on all components. Records cannot extend classes, cannot add instance fields and are implicitly final, because they must be exactly their components, but they can implement interfaces, declare static members, instance methods, nested types and a compact constructor for validation. Records became standard in Java 16 with JEP 395.
Open with
From that one line, the compiler generates a final class Point that extends java.lang.Record. It has two private final fields, x and y, and a canonical constructor that takes both in declaration order and assigns them.
Then, if they want more
It generates accessors named after the components, x() and y(), not getX, and it generates equals, hashCode and toString from all components, so two Points with the same values are equal, hash the same, and print as Point with x equals 1, y equals 2 in brackets.
That makes records safe as map keys. == is still identity, though, because a record is an ordinary object.
The restrictions all come from the idea that a record is exactly its components: it cannot extend another class, since it already extends Record, it cannot declare extra instance fields or instance initializers, and it is implicitly final so nobody can add state in a subclass.
What it can do is implement interfaces, declare static fields and static methods, instance methods that compute from the components, and nested types, and I can write a compact canonical constructor to validate or normalize the components, or copy a mutable list. Records were previewed in 14 and 15 and became final in Java 16 with JEP 395.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Hibernate 6, Java 14, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does the compiler generate for record Point(int x, int y) {}, and what restrictions...": https://scrivio-interview-library.netlify.app/java/records/5bcfa4c07b/