Exploring Record Classes in Java

Introduction: In this article, we will learn about record classes in Java. In Java 14, a new feature was introduced called record classes. This feature was officially added as a permanent feature in Java 16. Record classes aim to reduce boilerplate code associated with creating simple data carrier classes, also known as POJOs (Plain Old Java Objects).

What are Record Classes?

Record classes are a short and clear way to declare classes that are primarily used to store and transport immutable data. They automatically generate common methods like constructors, getters, equals(), hashCode(), and toString().

Syntax and Structure of a Record Class

public record Person(String name, int age) {
}

In the above example, Person is a record class with two fields: name and age.

  1. Working of Record classes: Let's see what happens when you declare a record class like the Person class above.
  2. Canonical Constructor: The record class automatically provides a constructor that takes arguments for each field.
  3. Getters: For each field, a getter method is automatically generated. The getter methods do not follow the usual getFieldName() naming convention but instead use the field name directly:
  4. equals(), hashCode(), and toString() Methods: These methods are overridden to provide behavior that is based on the record's components.

Example: Let us see an example of a record class.

public class RecordExample {
    public static void main(String[] args) {
        Person person = new Person("Alice", 30);
       
        // Accessing fields using getter methods
        System.out.println("Name: " + person.name());
        System.out.println("Age: " + person.age());
        
        // toString() method
        System.out.println(person);
        
        // equals() and hashCode() methods
        Person person2 = new Person("Alice", 30);
        System.out.println("Are persons equal? " + person.equals(person2));
        System.out.println("Hash codes: " + person.hashCode() + ", " + person2.hashCode());
    }
}
record Person(String name, int age) {
}

Benefits of Record Classes

  1. Reduced Boilerplate: Record classes minimize the amount of boilerplate code, making the code more concise and readable.
  2. Immutability: Record classes promote immutability, leading to safer and more predictable code.
  3. Automatic Implementations: Methods like equals(), hashCode(), and toString() are automatically provided, reducing the likelihood of errors.

Limitations of Record Classes

  1. Immutability Restriction: Record classes are designed for immutable data. If mutability is required, regular classes should be used instead.
  2. No Inheritance: Record classes cannot extend other classes (they implicitly extend java.lang.Record), although they can implement interfaces.

Summary

Record classes are a powerful feature in modern Java, offering a streamlined approach to creating simple data carriers. They reduce boilerplate, enhance code readability, and encourage immutability, making them a valuable tool for Java developers.


Similar Articles