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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:38:38+00:00 2026-06-09T21:38:38+00:00

I have a query regarding creating a Immutable class. Following are the points which

  • 0

I have a query regarding creating a Immutable class. Following are the points which I take in consideration:

  1. Make the class final
  2. Make all members final, set them explicitly, in a static block, or in the constructor
  3. Make all members private
  4. No Methods that modify state
  5. Be extremely careful to limit access to mutable member components(remember the field may be final but the object can still be mutable. ie private final Date imStillMutable) – See defensive copying or its cousin copy constructors for more info.

But I did not understand the 5 point completely at all, could you please advise or show me a example in which the 5 point is clear in that example?

  • 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-09T21:38:39+00:00Added an answer on June 9, 2026 at 9:38 pm

    Point 5 suggests that any time you have any methods which would return something to do with a mutable object, you’d want to create a copy which is independent of the private state. For example:

    public final class Foo
    {
        private final List<String> strings;
    
        public Foo(List<String> strings)
        {
            // Defensive copy on construction. Protects from constructing
            // code mutating the list.
            this.strings = new ArrayList<String>(strings);
        }
    
        public List<String> getStrings()
        {
            // Defensive copy on read. Protects from clients mutating the list.
            return new ArrayList<String>(strings);
        }
    }
    

    Note that the defensive copying is only required when the state is mutable. For example, if you used an ImmutableList (e.g. from Guava) as the state in the above class, you’d need to create a new list on construction (unless the input is also an ImmutableList) but not in getStrings.

    Also note that in this case String is immutable, so we don’t need to copy each string. If this were a List<StringBuilder> we’d need to create a new list and a new copy of each element as part of the defensive copy. As you can see, life becomes simpler when all your state is immutable too.

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

Sidebar

Related Questions

I have a few queries regarding columns in Postgres CSV Log. Query 1 Following
I have a query regarding showing a video (which is stored on server) on
I have been having a query regarding writing unit tests for web methods which
I have a query regarding the directory returned from Path.GetTempPath() function. It returns C:\Documents
I have a query regarding PLSQL exceptions and how to handle them in Java
I have a query here regarding the value given to the update attribute of
I have query table copyQuery which has Soil Condition has one of the columns,
I have query regarding the disposing of an object. The scenario is as follows.
I have one query regarding the (UITextfield and Webservice) for example When i start
I'm building an ASP.Net website. I have a cart class which stores the items

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.