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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:24:33+00:00 2026-06-11T10:24:33+00:00

With an immutable object, where is it proper to wrap a contained collection as

  • 0

With an immutable object, where is it proper to wrap a contained collection as unmodifiable? I see 3 options:

  1. In the immutable object’s factory:

    public class ImmutableFactory {
    
        public Immutable build(){
           List<Integer> values = new ArrayList<Integer>();
    
            values.add(1);
            values.add(2);
            values.add(3);
    
            return new Immutable(Collections.unmodifiableList(values), "hello");
        }
    }
    
  2. In the immutable’s contructor

    public class Immutable {
    
        private final List<Integer> values;
        private final String hello;
    
        public Immutable(List<Integer> values, String hello) {
            this.values = Collections.unmodifiableList(values);
           this.hello = hello;
        }
    
        public List<Integer> getValues() {
            return values;
        }
    
        public String getHello() {
            return hello;
        }
    }
    
  3. In the immutable’s accessor (if applicable).

    public class Immutable {
    
        private final List<Integer> values;
        private final String hello;
    
        public Immutable(List<Integer> values, String hello) {
            this.values = values;
            this.hello = hello;
        }
    
        public List<Integer> getValues() {
            return Collections.unmodifiableList(values);
        }
    
        public String getHello() {
            return hello;
        }
    }
    

Are there any other options and which one is proper?

  • 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-06-11T10:24:34+00:00Added an answer on June 11, 2026 at 10:24 am

    I’d say if you create Immutable collection then you need to protect yourself from somebody modifying list that was given as an argument to constructor, you should protectively copy it.

    public Immutable(List<Integer> values, String hello) {
        this.values = Collections.unmodifiableList(new ArrayList<Integer>(values));
        this.hello = hello;
    }
    

    I personally have long ago switched to Guava collections as there you can find interfaces for immutable collections. Your constructor would look like that:

    public Immutable(ImmutableList<Integer> values, String hello) {
        this.values = values;
        this.hello = hello;
    }
    

    You’d be sure that argument you receive will not be modified by anyone.

    It is usually a good idea to keep reference to immutable list within your immutable class as it then guarantees that you don’t modify it by mistake.

    Case (1) in my opinion makes sense only when factory method is the only way to create the immutable collection and even then it may break when someone refactors these classes. Unless there are other limitations it is always best to create self-sufficient classes.

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

Sidebar

Related Questions

there is an immutable class: Scope<Cmp extends Comparable<Cmp>> public Scope<Cmp> crop(Scope<Cmp> scope) { ...
I have the following attempt at an immutable object: class MyObject { private static
I have an immutable object that doesn't have its fields or class marked as
I see a article about the immutable object. It says when: variable = immutable
I want to have immutable Java objects like this (strongly simplified): class Immutable {
How does the name of an immutable object rebind to the result of an
There are immutable types in Java. And if you pass an immutable object to
Is it possible for a .NET web service to accept an immutable object (or
I am getting a problem, while inserting an object into the main List. [editContactList
It is safe to assume that working with or passing around an immutable object

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.