Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Every time you create a data-holding class in Java (like a DTO), you write the same boilerplate: private fields, constructor, getters, equals(), hashCode(), toString(). That's 30+ lines of code. Records do ALL of that automatically in ONE line.
Real World Analogy
Filling a physical form vs a digital auto-fill form. Physical: write name, address, phone 5 times on different sections. Digital: fill once, auto-fills everywhere. Records are Java's auto-fill for data classes.
The Problem Records Solve
java
QUBITS OF DPK
Record — Same in ONE Line!
java
QUBITS OF DPK
What Java Auto-Generates
javascript
QUBITS OF DPK
Records Are Immutable
java
QUBITS OF DPK
Records are implicitly final and their fields are private final.
Perfect for immutable data transfer objects.
Custom Compact Constructor
java
QUBITS OF DPK
Adding Methods to Records
java
QUBITS OF DPK
Records with Sealed Classes (Power Combo!)
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
30-Second Interview Answer
"Records introduced in Java 16 (stable in 17) are immutable data classes that automatically generate private final fields, canonical constructor, accessors (named after field, not getX style), equals, hashCode, and toString. They eliminate boilerplate for DTOs and value objects. Records are implicitly final and cannot extend other classes but can implement interfaces. Custom validation goes in the compact constructor. Key trap: accessors are x() not getX(). Used heavily for DTOs, API responses, events, and value objects in DDD."
Interview Questions & MAANG-Level Answers
Q1. What is a Record in Java and what does it auto-generate?
Record (Java 16+, stable Java 17) is a concise data class for immutable data carriers. record Point(int x, int y) {} auto-generates: (1) private final fields x and y. (2) Public canonical constructor Point(int x, int y). (3) Public accessor methods x() and y() (NOT getX()!). (4) equals() based on all components. (5) hashCode() based on all components. (6) toString() like Point[x=3, y=5]. Records are implicitly final and extend java.lang.Record.
Q2. How are record accessors named differently from regular getters?
Record accessors use the field name directly WITHOUT the "get" prefix: record Person(String name, int age) — accessors are person.name() and person.age(), NOT person.getName(). This is a common interview trap and a frequent source of bugs when migrating from POJOs to records. Frameworks like Jackson handle this automatically with @JsonProperty or by configuring the object mapper to recognize record-style accessors.
Q3. Can a record extend another class?
No. Records implicitly extend java.lang.Record and Java doesn't support multiple class inheritance. record Foo(int x) extends Bar {} — compile error. However, records CAN implement interfaces:
java
QUBITS OF DPK
This makes records work well with sealed interfaces for algebraic data type modeling.
Q4. What is a compact constructor in a record?
A compact constructor is a special record constructor that automatically receives all component values but doesn't need to explicitly assign them — Java does it automatically after the body runs:
java
QUBITS OF DPK
Perfect for validation and normalization without boilerplate.
Q5. What is the difference between a record and a regular immutable class?
A regular immutable class requires manually writing: private final fields, constructor, getters, equals, hashCode, toString — typically 30-50 lines. A record does all of this in ONE line. Behavioral difference: records are transparent (all components exposed), while regular classes can have private state not in the constructor. Records are for pure data carriers; regular immutable classes for richer value objects with complex invariants or private implementation details.
Q6. Can records implement interfaces?
Yes — this is the primary extension mechanism for records:
java
QUBITS OF DPK
This combination of sealed interfaces + records is the Java way of expressing algebraic data types (like Haskell/Scala), enabling exhaustive pattern matching.
Q7. When would you use a record vs a regular class?
Use record when: pure data carrier (DTO, value object, API response), all fields should be public and final, simple equality based on all fields, no complex behavior or mutable state needed. Example: record UserDTO(long id, String name, String email). Use regular class when: need mutable state, need to hide some fields from equals/hashCode, need to extend another class, complex behavior beyond data access, or need different access patterns.