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.