What is Record in Java? What problem does it solve?

pradz13

Pradipta

Posted on June 11, 2022

What is Record in Java? What problem does it solve?

For any data intensive applications, we write Java classes that can hold the data as per our requirement.

We need to define the following in most cases :

  • Define the variables

  • Define the constructor

  • Define the getter and setter methods

  • Override equals() and hashCode() methods

  • Override the toString() method

Although some of these can be auto-generated from IDEs, but lines of code increases with the increasing number of data elements.

Record comes to the rescue and reduces all the boilerplate code we had to write thus far. Record was introduced in Java language as a preview feature in Java SE 14.

How to define a record?

public record EmployeeRecord(String name, int employeeNumber) {
}
Enter fullscreen mode Exit fullscreen mode

Records can be instantiated like a class and attributes can be accessed by getter methods[please note that the the getter method names do not start with getXXXX()]

public class RecordExampleTest {
    public static void main(String[] args) {
        EmployeeRecord employeeRecord = new EmployeeRecord("Pradipta", 123);
        System.out.println(String.format("Employee Name : %s , Employee Number : %s",
                employeeRecord.name(), employeeRecord.employeeNumber()));
    }
}
Enter fullscreen mode Exit fullscreen mode

Constructor gets automatically generated which is known as Cannonical Constructor. The methods like toString(), equals() and hashCode() methods are also automatically generated implicitly.

Some points to remember :

  • Instance methods and static methods can be created inside a Record.

  • Static fields can be declared inside a Record.

  • Instance fields cannot be declared inside a Record.

  • Records cannot extend any other class as it implicitly extends Record class. As Java does not support Multiple Inheritance, Records cannot extend any other class.

  • Records are implicitly final - they cannot be extended by any other classes.

  • Records can implement interfaces.

  • Although Records provide Cannonical constructor by default, custom constructors can be created as well. It is not necessary to mention the arguments and set them explicitly if all the arguments to be set as mentioned in the Record. This is also known as Compact Constructor. Compact Constructors may be useful if some custom logic needs to be implemented.

The points mentioned above can be represented like below :

public record EmployeeRecord(String name, int employeeNumber) {
    private static final String DEFAULT_VALUE = "default-value";

    public EmployeeRecord {
        if(employeeNumber < 0)
            throw new IllegalStateException("Employee Numbers cannot be negative");
    }
    public String generateUniqueNumber() {
        return name + "-" + employeeNumber;
    }

    public static void printSomething() {
        System.out.println("Print anything here...." + DEFAULT_VALUE);
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
pradz13
Pradipta

Posted on June 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related