Hacking Hunter

A publication which gives general knowledge about software world

Follow publication

Java Records A Deep Look

Bergini Francis
Hacking Hunter
Published in
4 min readMar 18, 2025

Friend Link

We are going to explore the following items throughout the article.

The programming world is impressed and pays attention to the immutability aspect of data due to its thread-safe quality, To leverage that aspect java also started to introduce immutability in many of its members, for example in Date and Time, Optionals, and Streams. Immutability enables programs to execute lock-free, therefore increasing performance.

To make Java programs more out of immutability Java added a new member which is Record.

What it is?

A special purpose class holds two or more data and provides immutability, which means once declared the record has the same data encapsulated within it throughout its life cycle. Not only that, it provides getter methods and a canonical constructor by default having parameters that we provided in the record declaration.

Let’s look at a simple declaration

record Employee(String name, int age){};

That's it, this simple code provides you with the following auto-generated component. Notice it has no code in its body. (you can have a body for your advanced uses)

  1. private final String name.
  2. private final int age.
  3. getter methods with the name identical to the properties provided in the definition. name() and age().
  4. A canonical constructor looks like the following if we declare it in a class.
public Employee(String name, int age){
this.name = name;
this.age = age;
}

So, we can instantiate an object only with the record declaration with nobody, the same as we do in the case of class.

Employee emp = new Employee("Sri", 29);

These are enough to make a lazy developer happy. And what about the enthusiastic like you and me.

But before moving to the next heading, what about setters?

Dude! Remember the generated fields’ signatures, those are private final. Setter methods have nothing to do with them and as the purpose of record imply, those are for holding immutable data. Just to remind you who a record is.

-Hey lazy guys your class is over you can go now.

-Thank you sirrrrrrrrrrr.

Enthusiastic?

Some rules first

  1. A record cannot inherit another class.
  2. But implicitly it inherits the Record class declared in java.lang.Record
  3. Therefore have the abstractions of equals(), toString(), and hashCode() methods from the Object class.
  4. Based on your declaration the methods mentioned in the previous point from the object class are implemented automatically for you.
  5. A record cannot be extended as they are considered final.
  6. it can implement interfaces as same as classes do.
  7. we can override equals() and hashCode() methods from the object class inside the record’s body.
  8. All fields other than what we declared in the record definition should be static.

Enough of rules?

Let’s look at the advanced things we can do inside a record’s body

Canonical constructors and Compact record constructors.

  1. Canonical constructors.

— Knock Knock

Who is that?

I am one from the lazy group

what are you doing without going home?

I have a doubt, the minimal definition of record provides a predefined implementation for canonical constructor so why are you guys talking about it again?

Dude you must be in this group don’t go home stay here.

Even though the question is a fact, there are reasons that we have to implement the full form of canonical constructors again in the record’s body.

We have no setters here, but what is the purpose of setters in a class definition, if you are not confident

let’s carefully remember what Encapsulation is.

Encapsulation is a concept that group together data and behaviours act on it, and hide that bundled part from rest of the code and provide well defined interfaces to access and modify data.

this definition is a high-level of encapsulation, a class is a simple encapsulation emphasized example in Java where the accessing part is handled by getters and modifying part is handled by setters, most of the time the setters are for just setting the provided parameter in the private field of the object but its real purpose is to ensure the parameter provided in the setter method is eligible to be assigned. if yes assign if no, inform the user and leave the private field alone.

for example, if you have a month field declared private int and some one is trying to assign it a value more than 12 or less than 1.

But who is going to do it in records?

As records’ fields are private final, the only one who can do it is a constructor who can initialize the private final variables first and only time. So we can define such validity checkups before the values are assigned to the variables.

So in our case, we can check if the name of the employee has preceding and trailing spaces or age is greater than a certain number, etc. This is the purpose of redeclaring the canonical constructor again. Make sure while you declare the canonical constructor the signature should be the same as the record declaration.

public record Employee(String name, int age) {
public Employee(String name, int age){
//validity checks and assign the final fields here
}
}

Compact record constructor

Nothing special in compact constructer than canonical, we can declare it without the parameters and the rest of the implementations are the same.

public record Employee(String name, int age) {
public Employee {
//validity checks and assign the final fields here
}
}

For the variables inside the constructor, you should use the same name as you declared it in the record definition and the values you passed into the constructor would be assigned automatically.

Thats it?

Yes.

Only instance methods are allowed other than that method declarations are going to be the same as classes.

Bye.

Sign up to discover human stories that deepen your understanding of the world.

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

Hacking Hunter
Hacking Hunter

Published in Hacking Hunter

A publication which gives general knowledge about software world

Bergini Francis
Bergini Francis

Written by Bergini Francis

My articles are mix of life lessons, people, world, science, mind, spirituality, computer science and sewing.

No responses yet

Write a response