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 638831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:46:32+00:00 2026-05-13T20:46:32+00:00

I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =

  • 0

I have this class:

public MyClass {
    public void initialize(Collection<String> data) {
        this.data = data; // <-- Bad!
    }
    private Collection<String> data;
}

This is obviously bad style, because I’m introducing a shared mutable state. What’s the preferred way to handle this?

  • Ignore it?
  • Clone the collection?
  • …?

EDIT: To clarify why this is bad, imagine this:

MyClass myObject = new MyClass();
List<String> data = new ArrayList<String>();
myObject.initialize(data); // myObject.data.size() == 0
data.add("Test"); // myObject.data.size() == 1 

Just storing the reference poses a way to inject data into the private field myObject.data, although it should be completely private.

Depending on the nature of MyClass this could have serious impacts.

  • 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-13T20:46:32+00:00Added an answer on May 13, 2026 at 8:46 pm

    The best way is to deep clone the parameter. For performance reasons, this is usually not possible. On top of that, not all objects can be cloned, so deep copying might throw exceptions and cause all kinds of headache.

    The next best way would be a “copy-on-write” clone. There is no support for this in the Java runtime.

    If you think that it’s possible someone mutates the collection, do a shallow copy using the copy constructor:

    this.data = new HashSet<String> (data);
    

    This will solve your problem (since String is immutable) but it will fail when the type in the set is mutable.

    Another solution is to always make the sets immutable as soon as you store them somewhere:

    Set<String> set = ...
    ...build the set...
    
    // Freeze the set
    set = Collections.unmodifiableSet(set);
    
    // Now you can safely pass it elsewhere
    obj.setData (set);
    

    The idea here is turn collections into “value objects” as soon as possible. Anyone who wants to change the collection must copy it, change it and then save it back.

    Within a class, you can keep the set mutable and wrap it in the getter (which you should do anyway).

    Problems with this approach: Performance (but it’s probably not as bad as you’d expect) and discipline (breaks if you forget it somewhere).

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

Sidebar

Ask A Question

Stats

  • Questions 427k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This would ensure that < is only ever removed from… May 15, 2026 at 1:06 pm
  • Editorial Team
    Editorial Team added an answer I recommend tracking down the executable in the ClickOnce cache… May 15, 2026 at 1:06 pm
  • Editorial Team
    Editorial Team added an answer Links work as blocks: <% link_to '', my_path do %>… May 15, 2026 at 1:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.