Clean • Professional
Records are one of the most important modern features in Java. They were introduced as a preview in Java 14 and became a standard feature in Java 16.
Records provide a clean, concise, and efficient way to create immutable data classes. They are mainly used to store data without writing repetitive boilerplate code like getters, constructors, equals(), hashCode(), and toString().
A record is a special type of class designed to store immutable data. It provides a compact way to define data-carrying classes without writing repetitive code.
In simple words: A record is a short and clean way to create a class whose main purpose is to hold data.
Basic Syntax
record Person(String name, int age) {}
👉 That’s it! Java automatically generates everything for you.
When you create a record, Java automatically generates all the essential methods and fields required for a data class. This removes the need to write repetitive boilerplate code.
name() and age())equals() methodhashCode() methodtoString() methodExample
record Person(String name, int age) {}
public class Main {
public static void main(String[] args) {
Person p = new Person("Amit", 25);
System.out.println(p.name());
System.out.println(p.age());
}
}
👉 You can access values using methods like name() and age() instead of traditional getters.

Before records, creating a simple data class in Java required writing a lot of boilerplate code like constructors, getters, equals(), hashCode(), and toString(). This made the code lengthy and harder to maintain, especially in large applications where many such classes are needed.
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; }
public boolean equals(Object o) { ... }
public int hashCode() { ... }
public String toString() { ... }
}
With Records
record User(String name, int age) {}
👉 Java automatically handles all boilerplate code internally.
Records in Java come with several built-in features that make them powerful and easy to use for creating data-carrying classes. They reduce boilerplate code and improve readability while keeping the structure clean and simple.
All fields in a record are final by default, meaning their values cannot be changed after object creation.
p.name = "Rahul"; // Not allowed
Records remove the need to manually write common methods like constructors, getters, and utility methods.
👉 Instead of writing a long class, you can define everything in a single line.
record User(String name, int age) {}

Java automatically generates useful methods like getters, toString(), equals(), and hashCode().
User u = new User("Amit", 25);
System.out.println(u.name()); // getter
System.out.println(u.toString()); // string representation
System.out.println(u.equals(new User("Amit", 25))); // comparison
Records are mainly designed to carry data only, not to handle complex business logic.
👉 They are best used for simple data transfer between layers (like DTOs).
record Product(String name, double price) {}
Product p = new Product("Laptop", 55000);
System.out.println(p.name() + " - " + p.price());
You can also add validation logic using a compact constructor.
record Person(String name, int age) {
public Person {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
}
👉 This is useful for validating data during object creation.

Records can also contain custom methods just like normal classes. These methods help you add behavior related to the data stored in the record.
record Person(String name, int age) {
public String greeting() {
return "Hello " + name;
}
}
👉 This shows that even though records are mainly for data, you can still add useful methods when needed.
Records can also contain static members, which belong to the class rather than the object.
record Test(int x) {
static int count = 0;
}
👉 Static members are shared across all instances of the record.
Records are mainly used in scenarios where you need to store and transfer data in a clean and efficient way. They are especially useful in modern Java applications like APIs and microservices.
Example
record Product(String name, double price) {}
public class Main {
public static void main(String[] args) {
Product p = new Product("Laptop", 50000);
System.out.println(p);
}
}
👉 Records are very useful when you just need a simple data container without complex logic.
Records make Java code cleaner and more efficient, especially when working with simple data objects.
Although records are powerful, they have some restrictions because they are designed only for data storage.
java.lang.Record.👉 Records are mainly data carriers, not full-featured business classes.
| Feature | Record | Normal Class |
|---|---|---|
| Boilerplate | Very low (auto-generated code) | High (manual code required) |
| Mutability | Immutable by default | Can be mutable or immutable |
| Inheritance | Not allowed | Fully supported |
| Use Case | Data holder (DTOs, responses) | General-purpose logic classes |

Records should be used when your main requirement is to store and transfer data in a simple and clean way. They are best suited for lightweight data models where behavior is minimal.
Records are not suitable in scenarios where you need more flexibility or complex behavior.
Records in Java provide a modern and efficient way to create clean, concise, and immutable data classes. They remove the need for writing repetitive boilerplate code and make Java development more productive.
They are especially useful in modern applications where simplicity, clarity, and data-focused design are important.
👉 Overall, records are an important feature for modern Java development, clean coding practices, and technical interviews.