~/ learn/ java-from-zero/ cards/ Immutability & defensive copies
1 of 4

Type a defensive copy in a constructor

Type a defensive copy in a constructor

Answer

public Schedule(List<String> tasks) { this.tasks = new ArrayList<>(tasks); }

Copying the incoming list ensures that later mutations by the caller to their original list have no effect on the Schedule's internal state.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/09b46749-f44e-4ab0-bd8c-8621159f41b7/flashcard utf-8 LF