Records
Hub › Java › Beginner › Records
Goal
Declare a Java record and instantiate it. After this page you will know how to define the response shape our controller will return.
Prerequisites
What a record is
A record is a class whose only job is to hold a fixed set of fields. You declare the fields once in the header and Java generates the constructor, the field accessors, equals, hashCode, and toString for you.
Before records (Java 14 and earlier), the same data class required a constructor, two getters, an equals, a hashCode, and a toString — often 40+ lines for a 2-field class. A record collapses that to one line.
We use records here because the JSON we return on the exit page maps directly to record fields: one record, one JSON object, no boilerplate.
For the full feature spec, see the JEP 395 — Records. For this tier we only need the single-line form below.
Code
public class Items {
record Item(int id, String name) {}
public static void main(String[] args) {
var first = new Item(1, "notebook");
System.out.println(first);
System.out.println("id=" + first.id() + " name=" + first.name());
}
}Run it:
java Items.javaOutput:
Item[id=1, name=notebook]
id=1 name=notebookWhat to notice:
- The record header
Item(int id, String name)declares two fields. That is the whole class body — the{}is empty. new Item(1, "notebook")calls the auto-generated constructor.first.id()andfirst.name()are the auto-generated accessors. They are notgetId()andgetName()— records drop thegetprefix.- The auto-generated
toStringprintsItem[id=1, name=notebook].
For frontend developers
A record is the Java equivalent of a TypeScript type Item = { id: number; name: string } plus a constructor — a data shape with no behavior. You will use it the same way: to describe what a function returns.
Next → Collections