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

  • Home
  • SEARCH
  • 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 7668775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:26:36+00:00 2026-05-31T15:26:36+00:00

I am reading Effective Java by Joshua Bloch, item 39 make defensive copy, and

  • 0

I am reading “Effective Java” by Joshua Bloch, item 39 make defensive copy, and I have some questions. I always use the following construct:

MyObject.getSomeRef().setSomething(somevalue);

which is short for:

SomeRef s = MyClass.getSomeRef();
s.setSomething();
MyObject.setSomeRef(s);

It always works, but I guess if my getSomeRef() was returning a copy then my shortcut would not work, how can I know if the implementation of MyObject is hidden if it is safe to use a shortcut or not?

  • 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-31T15:26:37+00:00Added an answer on May 31, 2026 at 3:26 pm

    You’re violating two rules of OO programming:

    • do not talk to strangers
    • encapsulation

    Note that these rules are just rules, and that they can, or even must be broken sometimes.

    But if some data is owned by an object, and the object is supposed to guarantee some invariants on the objects it owns, then it should not expose its mutable internal data structures to the outside. Hence the need for a defensive copy.

    Another often used idiom is to return unmodifiable views of the mutable data structures:

    public List<Foo> getFoos() {
        return Collections.unmodifiableList(this.foos);
    }
    

    This idiom, or the defensive copy idiom, can be important, for example, if you must make sure that every modification to the list goes through the object:

    public void addFoo(Foo foo) {
        this.foos.add(foo);
        someListener.fooAsBeenAdded(foo);
    }
    

    If you don’t make a defensive copy or return an unmodifiable view of the list, a caller could add a foo to the list directly, and the listener would not be called.

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

Sidebar

Related Questions

I have been reading Effective Java by Joshua Bloch and so far it really
I'm reading through Chapter 3 of Joshua Bloch's Effective Java . In Item 8:
I'm currently reading Effective Java by Joshua Bloch and Item 17 is 'Design and
I was reading Joshua Bloch's Effective Java Programming Language Guide . He explains static
I am reading Effective Java by Joshua Bloch. At the top of page 16,
I'm reading Effective Java Second Edition by Joshua Bloch and im confused by below
I've been reading Josh Bloch's 'Effective Java 2nd Edition'. Item 43 states 'Return empty
I'm reading the Effective Java book by Joshua Bloch. In the first chapter, he
So I've been reading Effective Java by Joshua Bloch and noticed two points which
I've been reading Effective Java and decided to try to put some of what

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.