Home » Java » Core Java » Java 14: Records

About Fahd Shariff

Fahd Shariff
Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.

Java 14: Records

Java 14 arrived a few weeks ago and introduces the Record type, which is an immutable data carrier class designed to hold a fixed set of fields. Note that this is a preview language feature, which means that it must be explicitly enabled in the Java compiler and runtime using the --enable-preview flag.

I’m going to jump straight in with an example of a Book record designed to hold the title, author, publish date and price of a book. This is how the record class is declared:

1
2
public record Book(String title, String author, LocalDate publishDate, double price) {
}

You can use javap to see the code that the compiler has autogenerated:

01
02
03
04
05
06
07
08
09
10
public final class Book extends java.lang.Record {
  public Book(java.lang.String, java.lang.String, java.time.LocalDate, double);
  public java.lang.String title();
  public java.lang.String author();
  public java.time.LocalDate publishDate();
  public double price();
  public java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
}

As shown above, the compiler has automatically generated the constructor, getter methods, hashCode, equals and toString, thus saving us from having to type a lot of boilerplate code.

However, records do not just save on typing. They also make your intent clear that you want to model an immutable data item as a group of related fields.

Compact Constructors for Field Validation

Now let’s say that you want to add validation and default values to your record. For example, you might want to validate that Book records are not created with negative prices or future publish dates. This can be done with a compact constructor as shown below:

01
02
03
04
05
06
07
08
09
10
11
12
13
public record Book(String title, String author, LocalDate publishDate, double price) {
 
  //compact constructor (no parameter list), used for validation and setting defaults
  public Book {
    if (price < 0.0) {
      throw new IllegalArgumentException("price must be positive");
    }
    if (publishDate != null && publishDate.isAfter(LocalDate.now())) {
      throw new IllegalArgumentException("publishDate cannot be in the future");
    }
 this.author = author == null ? "Unknown" : author;
  }
}

The compact constructor does not have a parameter list. It validates the price and publish date, and also sets a default value for the author. The fields that have not been assigned in this constructor (i.e. title, publishDate and price) are implicitly initialised at the end of this constructor.

Alternative Constructors and Additional Methods

Records allow you to define additional methods, constructors, and static fields, as shown in the code below. However, remember that semantically a record is designed to be a data carrier, so if you feel that are adding extra methods, it might be that you need a class instead of a record.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public record Book(String title, String author, LocalDate publishDate, double price) {
 
  // static field
  private static final String UNKNOWN_AUTHOR = "UNKNOWN";
 
  // compact constructor, used for validation and setting defaults
  public Book {
    if (price < 0) {
      throw new IllegalArgumentException("price must be positive");
    }
    if (publishDate != null && publishDate.isAfter(LocalDate.now())) {
      throw new IllegalArgumentException("publishDate cannot be in the future");
    }
    this.author = author == null ? UNKNOWN_AUTHOR : author;
  }
 
  // static factory constructor
  public static Book freeBook(String title, String author, LocalDate publishDate) {
    return new Book(title, author, publishDate, 0.0);
  }
 
  // alternative constructor, without an author
  public Book(String title, LocalDate publishDate, double price) {
    this(title, null, publishDate, price);
  }
 
  // additional method to get the year of publish
  public int publishYear() {
    return publishDate.getYear();
  }
 
  // override toString to make it more user friendly
  @Override
  public String toString() {
    return String.format("%s (%tY) by %s for £%.2f", title, publishDate, author, price);
  }
}

Published on Java Code Geeks with permission by Fahd Shariff, partner at our JCG program. See the original article here: Java 14: Records

Opinions expressed by Java Code Geeks contributors are their own.

(+2 rating, 2 votes)
You need to be a registered member to rate this.
5 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Thirumal
Thirumal
6 months ago

How to work with equals? if i don’t want to compare some variable?

Voytechs
6 months ago
Reply to  Thirumal

I believe then you have to override the equals method yourself and implement the equals logic you want.

Avi Abrami
6 months ago

Excellent explanation but would be better, in my opinion, if you added code showing how to work with the Book record in a sample application. Maybe show how a book shop application would utilize Book records?

Deepak
Deepak
1 month ago
Reply to  abramia

If you have such examples of book shop that utilize Book records . Do share the java code

Deepak
Deepak
1 month ago

1. Please post examples of Records in Java ( more Scenarios like Multithreaded )

2. Post examples of Project Lombok ?