Record

Record is a special kind of class which allow us to create immutable data classes by minimizing the need for repetitive boilerplate code.

Below is the record overall syntax:

public record Student(int studentID, Stirng name) {}

Above Record is equivalent to below class:

public final class Student {
private final int studentID;
private final String name;

public Student(int studentID, String name) {
this.studentID = studentID;
this.name = name;
}

public int studentID() {
return studentID;
}

public String name() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return studentID == student.studentID &&
name.equals(student.name);
}

@Override
public int hashCode() {
return java.util.Objects.hash(studentID, name);
}

@Override
public String toString() {
return "Student[studentID=" + studentID + ", name=" + name + "]";
}
}

Record allow us to create a class having:

  • Constructor: A record automatically generates a constructor with parameters matching its declared fields at the time of record creation. A default (no-argument) constructor is not created. If needed, you can define one explicitly.
  • Getters: It will follow the same naming convention as the fields they represent. For example, studentID() and name())
  • Inbuilt: it will have toString(), equals(), and hashCode() methods.

Record without arguments or fields:

Record without arguments will have constructor with no fields and can be used to represent a singleton-like data type or a marker object.

public record StudentRecord() {}

Above Empty record:

  • Has no fields
  • zero-argument constructor
  • Still inherits equals(), hashCode(), and toString() from java.lang.Record

Note: Since records are intended to be immutable data carriers, a default no-argument constructor is not generated. All components must be initialized at the time of record instantiation.

Records with Custom Constructors:

In addition to the canonical constructor, a record can have explicitly declared constructors to provide alternative ways of instantiating the object. In the example below, the Student record defines a no-argument constructor and a single-parameter constructor.

public record Student(int studentID, String name) {

// No-argument constructor with default values
public Student() {
this(1, "ABC");
}

// Constructor with one argument (age is not a component; just for demo purposes)
public Student(int age) {
this(2, "DEF");
}
}

Since records automatically generate accessor methods based on their components, this record will provide only two methods: studentID() and name().

Key Notes:

  1. The parameterized constructor does not require explicit variable declarations in its parameter list, as they are implicitly defined by the record declaration.
record Student(String name, int age) {

public Student {
if(age <= 0)
System.out.println("Error: Age is less than 1 !!!");
}
}

class PracticeRecord {
public static void main(String[] args) {
Student student = new Student("Steve", 0);
System.out.println(student);
}
}

2. static variables declaration allowed inside record.

record Student(String name, int age) {

// record allow us to declare static variables
static int i = 0;
}

3. Instance variables declaration NOT allowed inside Student record.

record Student(String name, int age) {

// Below instance variables declaration not allowed in record.
int j = 2;
}

We can define instance variables as part of record declaration as mentioned below:

record Student(String name, int age, int j) {}

4. record NOT allowed to extend class.

5. record allow us to implement interface.

// Allow to implement an interface.
record Student(String name, int age) implements Runnable{

@Override
public void run() {

}
}

6. We can create methods inside record:

record Student() {

// Method definition
public String message() {
return "Hello World !!!";
}
}

class PracticeRecord {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.message());
}
}

Imran Khan, Adobe Community Advisor, certified AEM developer and Java Geek, is an experienced AEM developer with over 12 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0