👋 Introduction
In Java, we often write classes just to hold data — commonly known as POJOs (Plain Old Java Objects). These classes can be repetitive with all the getters
, setters
, constructors
, and toString()
methods.
Wouldn’t it be great if Java could generate all that for you?
Well, that’s exactly what Records do!
Records in Java were introduced in Java 14 (preview) and became a standard feature in Java 16. They let you write clean, concise data-holding classes without boilerplate code.
Let’s explore how Records can make your Java code simpler and more readable.

🧩 What are Records in Java?
A Record is a special type of class that is meant to hold immutable data. When you define a Record, Java automatically creates:
- A constructor
get
methodstoString()
equals()
andhashCode()
All in one line of code!
🔸 Example: Traditional POJO vs Record
Traditional Java POJO:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class User { private final String name; private final int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "User{name='" + name + "', age=" + age + "}"; } // equals() and hashCode() can also be overridden } |
Same thing using a Record:
1 2 3 4 |
public record User(String name, int age) {} |
That’s it! Java does the rest for you.
🚀 How to Use Records
Records are perfect when:
✅ You want an immutable data class
✅ You want to reduce boilerplate code
✅ You only care about storing data, not behavior
Here’s another simple use case:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
record Point(int x, int y) {} public class Main { public static void main(String[] args) { Point p = new Point(3, 5); System.out.println(p); // Output: Point[x=3, y=5] System.out.println(p.x()); // Access field with accessor } } |
⚙️ Features of Records
- Immutable Fields: All fields are
final
and values cannot be changed. - Compact Syntax: Less boilerplate code.
- Predefined Methods: Java generates
toString()
,equals()
, andhashCode()
automatically. - Constructor Support: You can still define your own constructors if needed.
1 2 3 4 5 6 7 8 |
record Employee(String name, int salary) { public Employee { if (salary < 0) { throw new IllegalArgumentException("Salary must be positive"); } } } |
🔍 When Not to Use Records
- When your class needs to be mutable
- If it contains business logic or complex methods
- If you need inheritance (records can’t extend other classes)
🧾 Summary
Feature | Traditional POJO | Java Record |
---|---|---|
Boilerplate | A lot | Very little |
Immutable | Optional | Always |
Inheritance | Yes | No |
Use Case | Flexible | Data containers |
💡 Final Thoughts
Java Records are a great way to write cleaner code when you just need to store and access data.
They are part of modern Java (Java 16 and above), so if you’re starting with Java 21, you’re ready to use them today!
Try converting a few of your POJO classes to Records and see how much cleaner your code becomes.
Reference : Java Doc