Practice ยท 3 of 3
Make the aliasing bug impossible
A Team object was leaking its internal roster. Implement a leak-proof version:
1. static final class Team with a List<String> members field
2. Constructor takes List<String> initial and defensively copies it
3. add(String m) appends; members() returns an UNMODIFIABLE view
4. static List<String> freeze(List<String> src) returns a copy that the caller can then mutate without ever affecting any Team
Tests attempt both leak directions: outside-in (mutating the list after the ctor) and inside-out (mutating the returned view).
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Object model drills