Java records are a special type of immutable class which is used for data carrier. It reduces so much boilerplate code so that performance of carrying data between modules and classes especially for POJO (Plain Old Java Objects) and DTO (Data Transfer Object) is fast.
Table of Contents
What are Java Records?
The main objective of java records is to create data carrier classes. The classes whose intention is only for contain data and carry it between modules and classes especially for POJO (Plain Old Java Objects) and DTO (Data Transfer Object).Record is an immutable class. Java records first introduced was in Java SE 14 as first preview features. Then second preview features in Java SE 15 and the final version released in Java SE 16.
How to create a record in Java?
public record Customer(int id, String name, String address) {
}
Why it is so important?
The importance of records in Java because of the following:
- We use record in the scenario in which we have to store only and transfer the data across modules. for example database result, query result and information from a service.
- Without record, generally we create a class for storing and transferring data across modules, in which we have to override so many methods and constructors such a getters and setters, hashcode(), equals(). toString() as well as private final fields, public constructors etc.
- When we create a record then we no need to override all those methods. Java compilers automatically generate code for those methods.
- Avoiding to write above all those methods reduces boilerplate code and also improve the performance of data carrier.
Due to above reasons, importance of records in java improved.
Some important properties or features of Java records
- In record, we can have nested classes and interfaces
- We can have nested records also which will be implicitly static
- We can implement a record with interface
- We can create generic record class
- We can use as local record classes in java
- Records are serializable
- Records are immutable
Java Record vs Class
On the Basis of Properties | Class | Records |
Immutability | We can define mutable and immutable objects and setters method can change the value of object until fields are final | By default immutable. All fields are generated by compiler which is final and we cannot change the value of objects. |
Boilerplated Code | We have to define constructors, getters, equals(), hashCode() and toString() explicitly | Automatically generates constructors, getters, equals(), hashCode() and toString() methods |
Flexibility | We are very flexible to define mix of data that means can declaration of multiple behaviors, mutable and immutable objects and multiple constructors. | Record are limited to holding immutable data but code is very concise way. |
Inheritance | It can be extend other classes and also it can extend | It cannot extend other class nor be extended. It extends only Record class implicitly |
Constructors | We can define multiple constructors with different parameters. Also we can define own custom logic in constructors. All fields are optional to initialization. | We can have multiple custom constructors but it must delegates to canonical constructors. It must me initialized. |
Field Access | We can define fields as public, protected, private with custom getters and setters methods | In this case fields are final and private. Public getters and setters method automatically generated by java compiler. We can not access final fields directly |
Serialization | We can define custom serialization logic with the help of methods like ‘writeObject’ and ‘readObject’ | It is straightforward to serialize, just we have to implements serializable interface. but limited in customization. |
Java Records Examples
Below are sample files to show the Java records example.
Before Java records
We have created two classes, one is for traditional class for data carrier and other is for more modern class, i.e. with record keyword. If we analyze Customer class, overridden setters and getters, hashCode(), equals(), toString() and also created constructors. A lot of boilerplate code is written in this class.
After java records
If we analyze the CustomerRecord class, then only we have to record for the same result. Code length is absolutely very less. Right!
package programming.seekho;
import java.util.Objects;
public class Customer {
private int id;
private String name;
private String email;
public Customer(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public int hashCode() {
return Objects.hash(email, id, name);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
return Objects.equals(email, other.email) && id == other.id && Objects.equals(name, other.name);
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}
package programming.seekho;
public record CustomerRecord(int id, String name, String email) {
}
package programming.seekho;
public class RecordExampleDemo {
public static void main(String[] args) {
Customer cust = new Customer(1, "Ramesh", "Ramesh@gmail.com");
CustomerRecord custRecord = new CustomerRecord(1, "Ramesh", " Ramesh@gmail.com ");
System.out.println(cust);
System.out.println(custRecord);
}
}
Output:
Customer [id=1, name=Ramesh, email=Ramesh@gmail.com]
CustomerRecord[id=1, name=Ramesh, email= Ramesh@gmail.com]
What is Java Canonical Constructor?
In the case of records, if we do not create any constructors, compiler generates constructor with parameters which is passed to records. This is called Canonical Constructor.
As in the normal class generally, we have to create default or parameterized constructors. If we do not create, then Java compiler insert one default constructor automatically.
For eg:

By default, below constructor is created. So no need to create constructor with same parameter type. But we can create custom constructors also.

Some important points need to remember:
When we pass the parameters to record, by default it is final, i.e.
id, name and email are by default final so we cannot the change the value.
- We cannot declare instance variables in the record because record is final.
- But static variables can be declare.
- We can create instance method
- We can also create static method
- We can get the value by calling just field name i.e getName() is replaced with name() only in record.
- Record can’t extend any class, but it implements an interface.
We can create a multiple custom constructor inside a record.
What is Java Compact Constructor?
The main aim of Compact constructor is to validate and normalize the field value without repeating parameter names and types. It is the newly created concept in Java records.
For eg: Below is the example of compact constructor. In this scenario, validating email and not initializing the parameter value again.

FAQs
Is a Java record serializable?
Yes, Java record is serializable as normal class but it is less customizable.
When to use Java Records?
Java records are used when you want to just holding data and data carrier across modules or classes. It is generally used with POJO (Plain Old Java Object) and DTO (Data Transfer Object). For example: Database Result, Query Result etc.
What is the difference between @value and record?
@value annotation used to create immutable java beans and it needs Lombok plugin but record is not a bean, it is built in features added in java.
Which JDK introduced records?
Records first of all introduced in JDK 14 as preview features then in JDK 15 as second preview features and final version introduced in JDK 16.
Is a Java record immutable?
Yes, Java records are immutable and basically it is used for data carriers. We cannot change the value of java records.
Conclusion
In this article we have learnt about java records. We learnt how Java records used for data carrier. How it reduced boilerplate code. Now developer has not any burden for overriding lots of boilerplate codes so records are very helpful.
If you like this article, please share with your friends…
If you want to know in more depth, click here
Some Related Topics:
Keep Learning!