Introduction to Record Class in Java

luthfisauqi17

luthfisauqi17

Posted on October 2, 2022

Introduction to Record Class in Java

Record classes is first introduced in Java SE 14 as a preview feature to help model plain data aggregates with less code than normal classes. This feature is important because it can make your code cleaner, and besides that it can also make your work more effective.

Record class syntax

Below is the official definition of a record class from Oracle:

A record class declares a sequence of fields, and then the appropriate accessors, constructors, equals, hashCode, and toString methods are created automatically. The fields are final because the class is intended to serve as a simple “data carrier”.


To understand the definition more clearly, we will see an example of the implementation of the normal java class and also the implementation of the java record class.

Source code 1:



public class Person {

    private final String name;
    private final int age;

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

    public String name() {
        return name;
    }

    public int age() {
        return age;
    }

    @Override
    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, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person[" +
                "name=" + name +
                ", age=" + age +
                ']';
    }
}


Enter fullscreen mode Exit fullscreen mode

Source code 1 shows the Person aggregate class created using the normal Java classes with a standard implementation that usually all properties are final, has a constructor with parameters of all properties, accessors for each property, equals method, hashCode method, and toString method.

Source code 2:



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


Enter fullscreen mode Exit fullscreen mode

Source code 2 shows the implementation of the same Person aggregate class as in Source code 1, but uses the java class record. You can see that the same can be achieved using this feature with much less code.


Creating an object from a record class is exactly the same as creating an object from a normal java class, using the new keyword. An example of creating an object from a record class can be seen in Source code 3.

Source code 3:



Person person = new Person("Bob", 27);


Enter fullscreen mode Exit fullscreen mode

And if you try to use the toString method on the object as in Source code 4, you can see the output like the one in Output 1.

Source code 4:



Person person = new Person("Bob", 27);
System.out.println(person.toString());


Enter fullscreen mode Exit fullscreen mode

Output 1:



Person[name=Bob, age=27]


Enter fullscreen mode Exit fullscreen mode

Congratulations, you have just learned the record class feature in Java. There are still many things related to record classes that have not been discussed in this article such as how to create custom methods, how to create custom constructors, canonical constructors, etc. I will discuss these things in the next article, but you can also learn it yourself through the official documentation from Oracle: https://docs.oracle.com/en/java/javase/15/language/records.html


Cover image:

https://i.picsum.photos/id/955/1920/720.jpg?hmac=lnc4y4CILWRblCsnXbmfX0JmIEjni6rFGeo6JztfCJw

Other image:

https://marco.dev/assets/img/uploads/2020/record_definition.png

💖 💪 🙅 🚩
luthfisauqi17
luthfisauqi17

Posted on October 2, 2022

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

Sign up to receive the latest update from our blog.

Related