Java 17 — Record Classes

Prakash Karuppusamy
2 min readSep 28, 2024

Record is a special kind of class, designed to hold data with less boilerplate code. It’s a more concise way to define classes that only store data, removing the need for writing getters, setters, toString(), equals(), and hashCode() methods manually.

Git Link: https://github.com/prakashbtech87/PrakashJavaRepository/tree/main/src/main/java/com/prakash/version/java17/record

Here’s a comparison between your regular class and a Record class:

Regular class:

package com.prakash.version.java17.record.normalclass;

import java.util.Objects;

/**
* @author prakashkaruppusamy
*/

class Animal {
String name;
String animalType;
long runningSpeed;

// Constructor
public Animal(String name, String animalType, long runningSpeed) {
this.name = name;
this.animalType = animalType;
this.runningSpeed = runningSpeed;
}

// Getters
public String getName() {
return name;
}

public String getAnimalType() {
return animalType;
}

public long getRunningSpeed() {
return runningSpeed;
}

// toString() method
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", animalType='" + animalType + '\'' +
", runningSpeed=" + runningSpeed +
'}';
}

// hashCode and equals methods
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal animal = (Animal) o;
return runningSpeed == animal.runningSpeed &&
name.equals(animal.name) &&
animalType.equals(animal.animalType);
}

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

Record class

package com.prakash.version.java17.record;

/**
* @author prakashkaruppusamy
*/

public record Animal(String name, String animalType, long runningSpeed) {
}

How Records make life easier:

1. Less Boilerplate: You don’t need to manually write getters, setters, toString(), equals(), or hashCode(). All of these are automatically provided by the Record class.

2. Immutability: Once you create a Record object, its state cannot change. This makes it simpler to work with, especially in multithreading scenarios.

3. Conciseness: You write less code, which means fewer bugs and easier maintenance.

package com.prakash.version.java17.record;

/**
* @author prakashkaruppusamy
*/

public class Main {
public static void main(String[] args) {
Animal lion = new Animal("Lion", "Mammal", 50);

// Accessing data
System.out.println("Animal Name: " + lion.name());
System.out.println("Animal Type: " + lion.animalType());
System.out.println("Running Speed: " + lion.runningSpeed());

// Using toString() automatically
System.out.println(lion); // Output: Animal[name=Lion, animalType=Mammal, runningSpeed=50]
}
}

In summary, Records simplify your code by automatically generating most of the repetitive tasks involved in defining data-holding classes.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (2)

Write a response

2. Immutability

This depends on the data types your put into your record...

1. Less Boilerplate:

In a record there do not exists "setters"...