defensive copy

A copy of a mutable object, such as a list, made when it crosses a boundary so the other side cannot change your data.

In more detail

If a method returns its internal list, a caller can alter your state without going through any domain rule. Returning a copy, for example with List.copyOf, gives the caller its own unmodifiable collection. Decide and document who owns each mutable collection.

Example

return List.copyOf(items);

Related terms

Taught in

References