← Quiz: Immutability and Value Objects: all 10 questions
Quiz: Immutability and Value Objects · Question 3 of 10
Which line correctly makes a defensive, unmodifiable copy of a list parameter?
Which line correctly makes a defensive, unmodifiable copy of a list parameter?
- A.
this.items = items; - B.
this.items = (List<String>) items.clone(); - C.
this.items = List.copyOf(items); - D.
this.items = final items;
Answer & explanation
Correct answer: this.items = List.copyOf(items);
List.copyOf creates a new unmodifiable list, so later changes to the caller's list do not leak in.
Can you get the rest right without peeking?
Take the timed Quiz: Immutability and Value Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →