Skip to content
Ameen Abozaid
In reviewJNR-001-L08

Demo: modernizing a legacy Java class step by step

  • Lesson 8 of 8
  • 11 min read
  • Junior
On This Lesson 3 topics

The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.

In this lesson

turn an old getter/setter class into modern Java. Prepares JNR-001-LAB1.

Draft outline details

Step by step

  1. Show the legacy Product class: fields, getters, setters, equals, hashCode, toString.
  2. Run the existing tests so you have a safety net.
  3. Replace the class with a record; remove the setters and fix the callers.
  4. Planned mistake: the class had a List<String> tags. Passing a list that the caller changes later breaks immutability. Fix: copy it in the compact constructor with List.copyOf.
  5. Replace the if/else instanceof chain with a sealed interface and a switch.
  6. Run the tests again and list what you removed.

Check your understanding

Why copy the list?

Show answer

A record is shallow; a shared list can still be changed from outside.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.