What is a Data Class?
Learn what a data class is, how it generates equals/hashCode/toString, and how Java records implement it, with examples.
Expected Interview Answer
A data class is a class whose primary purpose is to hold state, with the compiler or language automatically generating boilerplate members like equals, hashCode, toString and accessors based on its fields.
Rather than hand-writing equals() and hashCode() that compare every field, or a toString() that prints them all, a data class declaration lets the compiler derive these mechanically and consistently from the declared properties. Java’s `record` keyword, Kotlin’s `data class`, and Lombok’s `@Data` annotation are all implementations of this idea. Because the generated equality is structural (field-by-field) rather than reference-based, two separate data-class instances with identical field values are considered equal. This makes data classes ideal for DTOs, value carriers and simple domain objects, but they are a poor fit for entities whose identity must remain stable even as fields change.
- Eliminates repetitive equals/hashCode/toString boilerplate
- Guarantees consistent, correct structural equality
- Improves readability for simple state-holding types
- Reduces bugs from forgetting to update generated methods when fields change
AI Mentor Explanation
A player’s official scorecard entry — runs, balls faced, fours, sixes — is judged identical to another entry purely by comparing those numbers, not by which physical piece of paper it’s written on. Two scorecards with the same figures are treated as the same record. A data class works this way: the compiler generates equality and printing logic purely from the field values, so two instances with identical data are considered equal automatically.
Step-by-Step Explanation
Step 1
Declare the fields
List the properties that make up the object's state (e.g. name, age).
Step 2
Mark it as a data class
Use the language construct — Java record, Kotlin data class, or Lombok @Data.
Step 3
Let the compiler generate boilerplate
equals(), hashCode(), toString() and accessors are derived automatically from the fields.
Step 4
Use structural equality freely
Two instances with the same field values compare equal via .equals(), safe for use in sets/maps.
What Interviewer Expects
- Correct explanation of structural vs reference equality
- A concrete implementation example (record, data class, or @Data)
- Awareness of when NOT to use a data class (mutable entities with identity)
- Mention of consistent equals/hashCode/toString generation
Common Mistakes
- Using a data class for an entity whose identity should be stable across field changes
- Forgetting that generated hashCode changes if a mutable field changes, breaking hash-based collections
- Assuming data classes are unique to one language (the pattern exists across many)
- Not knowing structural equality compares all declared fields, not a subset, unless customized
Best Answer (HR Friendly)
“A data class is a class that mainly exists to hold data, where the language automatically generates the repetitive equals, hashCode and toString methods based on its fields. This saves developers from writing and maintaining that boilerplate by hand and keeps equality checks consistent.”
Code Example
public record Point(int x, int y) {
// equals(), hashCode(), toString(), and accessors x()/y()
// are all generated automatically from the declared fields
}
Point p1 = new Point(3, 4);
Point p2 = new Point(3, 4);
System.out.println(p1.equals(p2)); // true, structural equality
System.out.println(p1); // Point[x=3, y=4]Follow-up Questions
- How does a data class differ from a regular class in terms of equality?
- What generated methods does a Java record provide?
- When would you avoid using a data class?
- Why must hashCode be consistent with equals?
MCQ Practice
1. A data class primarily automates the generation of?
Data classes generate structural comparison and display boilerplate from declared fields.
2. Two data class instances with identical field values are, by default, considered?
Generated equals() compares field values, so matching fields mean the instances are equal.
3. Data classes are generally a poor fit for?
Entities need identity-based equality that survives field changes, which structural equality does not provide.
Flash Cards
Data class in one line? — A class focused on holding state with auto-generated equals/hashCode/toString.
Java equivalent? — The record keyword.
Equality type used? — Structural (field-by-field), not reference-based.
When to avoid it? — For entities whose identity must remain stable independent of field values.