Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 08 / Topic 11 of 12

Records as plain, immutable data classes

A record declares a class whose entire job is to carry data, nothing else. Writing record TransactionRecord(String type, double amount, String date) gets you, for free: private final fields for each component, a constructor that takes them in order, an accessor method per component (named after the component, no get prefix), and toString()/equals()/hashCode() that all work field-by-field. There are no setters, ever — a record's fields cannot change after construction, which is the point. Think of a printed passbook entry: once the bank prints "DEPOSIT, ₹5000, 2026-11-01" into your passbook, that line does not get edited — a correction is a new line, never a change to the old one.

Real-world example: passbook entries as TransactionRecords, with a compact constructor for validation.

record TransactionRecord(String type, double amount, String date) {
    TransactionRecord {                        // compact constructor: validate before the fields are set
        Objects.requireNonNull(type, "type must not be null");
        if (amount <= 0) throw new IllegalArgumentException("amount must be positive, got " + amount);
    }

    static TransactionRecord deposit(double amount, String date) {
        return new TransactionRecord("DEPOSIT", amount, date);
    }

    static TransactionRecord withdrawal(double amount, String date) {
        return new TransactionRecord("WITHDRAWAL", amount, date);
    }
}
TransactionRecord[type=DEPOSIT, amount=5000.0, date=2026-11-01]
TransactionRecord[type=WITHDRAWAL, amount=1200.0, date=2026-11-05]
t1.amount() = ₹5000.0
t1.equals(t1Copy): true (records compare by value, field by field)
t1.hashCode() == t1Copy.hashCode(): true
net movement: ₹3800.0
rejected: amount must be positive, got -100.0

A compact constructor — TransactionRecord { ... } with no parameter list, because it reuses the record's own components — runs before the fields are assigned, so it is the place to validate or normalize; after it finishes, Java assigns every component for you. Compare the generated toString() here with section 5's hand-written one: a record gives you that Type[field=value, ...] shape automatically, which is exactly why equals/hashCode/toString are the three methods records exist to stop you from writing by hand for simple data. Reach for a record instead of a regular class whenever the thing you are modeling is just data with no identity of its own — a transaction line, a Weight, a coordinate pair; reach for a regular class (sections 1–8) when the object has real identity and mutable state across its lifetime, like a BankAccount whose balance changes while the account itself stays "the same account." (Stage 3 Part 14 returns to records once they can also implement a sealed interface — out of scope here, where records are deliberately kept to plain, standalone data.)

Full program: examples/RecordsDemo.java.

Done here? The practice set for Part 8 — including the solutions — is on the part page.

Search the guide