Defensive Copying

  • Read Effective Java – Item 50: Make defensive copies when needed. This reading mentions some concepts we have not covered in class, but you should be able to understand the core points.
  • defensive programming
  • invariants
  • malicious client
  • internal state of an object
  • defensive copy
  1. Think of an example of each of the following:
  2. class invariant
  3. immutable class
  4. non-final class
  5. window of vulnerability

  6. How do you determine whether an object is immutable? Is an immutable object always “safe?”
  7. What if you are writing code that is never called by or used by someone who is not on your team? Would you ever still want to practice “defensive programming” to defend against yourself?

What was particularly interesting, confusing, or exciting to you in this reading?


This comes from Effective Java. The whole book is an excellent one to read in its entirety. Is it essential if you plan to do serious work in Java after this class, and its excellent discussion of general programming principles will make you a better programmer no matter what language you are working in.

If you are curious to read more of the book, we have posted an optional additional excerpt for you:

  • Effective Java – Item 49: Check parameters for validity