advancedCore Java Fundamentals260 of 436
What is a defensive copy and why is it needed?
Tests whether you know returning or storing a direct reference to a mutable field (like a Date or array) lets external code mutate your object's internal state without going through its API -- a defensive copy on the way in or out closes that hole and is essential for genuinely immutable classes.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response