Java 16 - Introduction to Records

[Last Updated: May 16, 2021]

Java 16 enhances the programming language with records.
Records provide a compact syntax for declaring classes which are plain immutable data carriers.

Before Java 16
public class Person {
    private final String name;
    private final String gender;
    private final int age;

    public Person(String name, String gender, int age) { = name;
        this.gender = gender;
        this.age = age;

    public String getName() {
        return name;

    public String getGender() {
        return gender;

    public int getAge() {
        return age;

    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, &&
                Objects.equals(gender, person.gender);

    public int hashCode() {
        return Objects.hash(name, gender, age);

    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
In Java 16
public record Person(String name, String gender, int age) {}

Records are syntactical sugar which help us to avoid writing boilerplate code when creating data carriers.

Java Records have been preview feature in Java 14 (JEP 359) and Java 15 (JEP 384). JEP 395 finalized the feature in JDK 16.


Creating record


package com.logicbig.example;

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

Using record


package com.logicbig.example;

public class RecordExample {
  public static void main(String[] args) {
      Person person = new Person("Jenny", "Female", 35);
Person[name=Jenny, gender=Female, age=35]

Inspecting with javap

Let's use javap on the Person.class to see what it is compiled to.

$ javap Person.class
Compiled from ""
public final class com.logicbig.example.Person extends java.lang.Record {
public com.logicbig.example.Person(java.lang.String, java.lang.String, int);
public final java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String name();
public java.lang.String gender();
public int age();

As seen above record is compiled to a normal Java class having a constructor, accessors, equals(), hashCode() and toString() methods.

Also note that above compiled class is extended by java.lang.Record which is the common base class of all Java language record classes.

Example Project

Dependencies and Technologies Used:

  • Java 16
Java 16 - Record Example Select All Download
  • java-intro-to-records-example
    • src
      • com
        • logicbig
          • example

    See Also