Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:12+00:00 2026-05-25T09:53:12+00:00

In Josh Bloch’s excellent book Effective Java under Item 39 he says: [D]efensive copies

  • 0

In Josh Bloch’s excellent book Effective Java under Item 39 he says:

“[D]efensive copies are made before checking the validity of the parameters, and the validity check is performed on the copies rather than on the originals.”

The example given is as follows:

public Period(Date start, Date end) {
   this.start = new Date(start.getTime());
   this.end = new Date(end.getTime());

   if(this.start.compareTo(this.end) > 0)
      throw new IllegalArgumentException("...");
   }
}

The problem with doing the validity checks after the defensive copy is that an invalid parameter can cause the creation of the copy to fail. For example, the class above will throw a NullPointerException if you pass it a null for start or end.

If I move the validity check before the defensive copy, I am vulnerable to the time-of-check/time-of-use attack that Bloch cites as the reason for doing the defensive copy first.

My question is what is a way around this? I can’t believe I’m the first person to see this problem in a well read book (though the errata for the book says nothing about it), so maybe I’m just missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:53:13+00:00Added an answer on May 25, 2026 at 9:53 am

    As others said, you check for null before copying the parameters.

    If I move the validity check before the defensive copy, I am
    vulnerable to the time-of-check/time-of-use attack that Bloch cites as
    the reason for doing the defensive copy first.

    No, a hacker won’t be able to change a reference to an actual instance to a null reference, or vice-versa. The copying is done to avoid changes to the internal state of the arguments from another thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the book Effective Java, Josh Bloch says that StringBuffer is largely obsolete and
I've been reading Josh Bloch's 'Effective Java 2nd Edition'. Item 43 states 'Return empty
I am reading Josh Bloch's book Effective Java and he suggests using a builder
Item 3 of Josh Block's Effective Java (Enforce the Singleton Property With a Private
Take a look at this java puzzles vid by Josh Bloch and William Pugh,
Edited after getting answers Some excellent answers here. I like Josh's because it is
I'm using the method described by Josh in this question to add a toolbar
I've just been reading Josh Smith's MVVM article and am working on a WPF
I was reading a blog entry by Josh Smith where he used a cache
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.