How do I create a record to store data in Java?

A record is a concise way to define a class that holds data. Java automatically provides a constructor, accessor methods, and implementations of equals(), hashCode(), and toString().

In this example, you will create a Book record to store a title and an author.

Source code

Save the following code as RecordExample.java:

public class RecordExample {
    void main() {
        Book firstBook = new Book("Learning Java", "Alice");
        Book secondBook = new Book("Learning Java", "Alice");

        IO.println("Title: " + firstBook.title());
        IO.println("Author: " + firstBook.author());
        IO.println("Record: " + firstBook);
        IO.println("Equal values: " + firstBook.equals(secondBook));
        IO.println("Same object: " + (firstBook == secondBook));
    }
}

record Book(String title, String author) {
}

Explanation

The following declaration defines a record with two components:

record Book(String title, String author) {
}

The components are title and author, both of type String.

For this declaration, Java automatically provides:

Member Purpose
Book(String title, String author) Initializes the components
title() Returns the title
author() Returns the author
equals(Object other) Compares component values
hashCode() Calculates a hash code from the components
toString() Returns a text representation of the record

Creating a record object

Use the new keyword and pass a value for each component:

Book firstBook = new Book("Learning Java", "Alice");

The arguments follow the order of the components in the record declaration.

Reading component values

Record accessors use the component names directly:

firstBook.title()
firstBook.author()

The generated methods are named title() and author(), rather than getTitle() and getAuthor().

Each component has a private final field. Records do not generate setters, so you cannot reassign these fields after construction.

Comparing records

The example creates two separate objects with the same component values:

Book firstBook = new Book("Learning Java", "Alice");
Book secondBook = new Book("Learning Java", "Alice");

The generated equals() method returns true because both objects are instances of the same record class and have equal component values.

The == operator returns false because the variables refer to separate objects.

Printing a record

Passing the record to IO.println() uses its generated toString() method:

IO.println("Record: " + firstBook);

The result includes the record name, component names, and their values.

Run the example

Compile and run the file:

javac RecordExample.java
java RecordExample

Output

Title: Learning Java
Author: Alice
Record: Book[title=Learning Java, author=Alice]
Equal values: true
Same object: false

Are records immutable?

Records are shallowly immutable: their component fields cannot be reassigned, but referenced objects may still be mutable.

This Book record contains only strings, which are immutable. If a record contains a mutable list or array, additional steps are needed to protect that data from changes.

Leave a Comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.