In Java, the “record” was first introduced in Java SE 14 however it’s not a permanent feature of Java. Records make it easy to write complex longer code in a simple and precise way. As a result, the developer may write code quickly and with minimal effort. The code complexity also reduces. Records are only used for the classes that contain data. So, programmers can efficiently carry data between different modules.
This write-up will briefly discuss the “record” in Java.
What is a “record” in Java?
While writing a program, many classes only serve as data carrier classes to use that data between modules. Although the purpose of these classes is only to carry the data but to write these classes we need to write a lot of code that contains hashCode(), getter and setter methods, toString(), and equal() and a lot of effort and time will be required for this purpose. So, the record classes save this time and effort by allowing us to efficiently hold and aggregate the data.
The general syntax of a “record” class is described below:
record recordname(components to be used)
{...
}
record xyz(int id, String name)
{...
}
Now, check out the provided example to understand the “record” class concept in Java:
public class Student {
int reg_no;
String name;
public Student(int reg_no,String name)
{
this.reg_no = reg_no;
this.name = name;
}
public void setreg_no(int reg_no)
{
this.reg_no = reg_no;
}
public int getreg_no() { return reg_no; }
public void setname(String name)
{
this.name = name;
}
public String getname() { return name; }
public String toString()
{
return "Student reg_no= " + reg_no
+ ", and Name= " + name ;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student s = (Student) o;
return getreg_no() == s.getreg_no() && getname().equals(s.getname());
}
@Override
public int hashCode() {
final int prime = 20;
int result = 1;
result = prime * result + reg_no;
result = prime * result
+ ((name == null)
? 0
: name.hashCode());
return result;
}
public static void main( String[] args )
{
Student s1 = new Student(103,"Ali");
Student s2 = new Student(104,"Sana");
Student s3 = new Student(105,"Bilal");
Student s4 = new Student(106,"Amna");
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
System.out.println(s1.equals(s2));
}
}
In the above-stated code,
- We want to display the student’s registration no and names so we have created a “Student” class.
- Inside the “Student” class we have two variables “name” and “reg_no”.
- After that, we create the getter and setter methods for these two variables.
- Then, we have “hashCode()” and “equals” methods to compare different assigned strings.
- Lastly, in the “main()” method we created objects of “Student” as “s1”, “s2”, “s3” and “s4” and set the values of “name” and “reg_no” and print these values.
- We also use the equal method to compare the values of “s1” with “s2”.
Here is the output:

This is a long and complex code just to store some data so we can simply use only one record class instead of all these methods and thus reduce the complexity of the above-described code. The record class for this code will be:
record student(int reg_no, String name){...}
This class only holds the student’s data instead of all those methods. Now, let’s see the whole program with output:
class Student {
record student(int reg_no, String name){}
public static void main( String[] args )
{
student s1 = new student(103,"Ali");
student s2 = new student(104,"Sana");
student s3 = new student(105,"Bilal");
student s4 = new student(106,"Amna");
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
System.out.println(s1.equals(s2));
System.out.println(s1.name());
}
}
Output

Here, we have seen how one record class can make the code so simple and efficient.
Conclusion
In Java, the “record” class is used when users have code carrier classes in their code. It makes the implementation simple by holding the data between different modules. Additionally, the user can skip all the getter, setter methods, constructors, toString(), equals(), hashCode() methods and only implement the record class and can access all these methods without writing code for it. In this write-up, we have demonstrated on “record” class in Java.