Loading published curriculum…

04 · Java language in depth / LESSON 14 OF 102

Records, enums, and defensive copies

Immutable values reduce the number of states the rest of your program must defend against.

Before this lesson: equals, hashCode, and collection identity

Understand the topic

A record provides final component references and generated accessors, but it is only shallowly immutable. A mutable List component can still change. Copy collections on construction and expose immutable views or values.

Enums model a closed set of states. Prefer explicit enum transitions to magic strings. A compact record constructor can normalize inputs and reject invalid values before assigning its components.

EXAMPLE · JAVA

Main.java

import java.util.*;
public class Main {
  record Plan(List<String> tasks) {
    Plan { tasks = List.copyOf(tasks); }
  }
  public static void main(String[] args) {
    var input = new ArrayList<>(List.of("Java"));
    var plan = new Plan(input);
    input.clear();
    System.out.println(plan.tasks());
  }
}

What to expect

Prints [Java]; modifying the original list does not modify the record.

YOUR TURN

Put it into practice

  1. 01Create a validated TaskDraft record and a Status enum.
  2. 02Reject blank titles and copy tag collections before storing them.
  3. 03Verify: External list mutation does not change the draft and invalid construction fails immediately.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Immutable values reduce the number of states the rest of your program must defend against.

  1. 01A record provides final component references and generated accessors, but it is only shallowly immutable. A mutable List component can still change. Copy collections on construction and expose immutable views or values.
  2. 02Enums model a closed set of states. Prefer explicit enum transitions to magic strings. A compact record constructor can normalize inputs and reject invalid values before assigning its components.
  3. 03Trace the example: Prints [Java]; modifying the original list does not modify the record.

A mistake worth avoiding

A record changes when the caller edits its original list.

DEBUGGING ROOM

Something broke. Where do you look?

A record changes when the caller edits its original list.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does a final List field prevent add operations?

Choose one answer

BUILD IT YOURSELF

Create a validated TaskDraft record and a Status enum.

  1. 1.Create a validated TaskDraft record and a Status enum.
  2. 2.Reject blank titles and copy tag collections before storing them.
  3. 3.Reproduce this failure, then repair it: A record changes when the caller edits its original list.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does a final List field prevent add operations?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

External list mutation does not change the draft and invalid construction fails immediately.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references