Introduction to Record Class in Java
Tags: tutorial, beginners, programming, java
Published: 2022-10-02 08:38:26

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmit4m4g1b99escee35f.png)

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:***

```java
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 +
                ']';
    }
}
```

***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:***

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

***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:***

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

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:***

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

***Output 1:***

```
Person[name=Bob, age=27]
```

---

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](https://docs.oracle.com/en/java/javase/15/language/records.html)

---

Cover image:

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

Other image:

[https://marco.dev/assets/img/uploads/2020/record_definition.png](https://marco.dev/assets/img/uploads/2020/record_definition.png)