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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:11:10+00:00 2026-05-15T14:11:10+00:00

How can one make a Java class immutable, what is the need of immutability

  • 0

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?

  • 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-15T14:11:11+00:00Added an answer on May 15, 2026 at 2:11 pm

    What is an immutable object?

    An immutable object is one that will not change state after it is instantiated.

    How to make an object immutable?

    In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters.

    The following class will create an immutable object:

    class ImmutableInt {
      private final int value;
    
      public ImmutableInt(int i) {
        value = i;
      }
    
      public int getValue() {
        return value;
      }
    }
    

    As can be seen in the above example, the value of the ImmutableInt can only be set when the object is instantiated, and by having only a getter (getValue) the object’s state cannot be changed after instantiation.

    However, there must be care taken that all objects that are referenced by the object must be immutable as well, or it could be possible to change the state of the object.

    For example, allowing an reference to an array or ArrayList to be obtained through an getter will allow the internal state to change by changing the array or collection:

    class NotQuiteImmutableList<T> {
      private final List<T> list;
    
      public NotQuiteImmutableList(List<T> list) {
        // creates a new ArrayList and keeps a reference to it.
        this.list = new ArrayList(list); 
      }
    
      public List<T> getList() {
        return list;
      }
    }
    

    The problem with the above code is, that the ArrayList can be obtained through getList and be manipulated, leading to the state of the object itself to be altered, therefore, not immutable.

    // notQuiteImmutableList contains "a", "b", "c"
    List<String> notQuiteImmutableList= new NotQuiteImmutableList(Arrays.asList("a", "b", "c"));
    
    // now the list contains "a", "b", "c", "d" -- this list is mutable.
    notQuiteImmutableList.getList().add("d");
    

    One way to get around this problem is to return a copy of an array or collection when called from a getter:

    public List<T> getList() {
      // return a copy of the list so the internal state cannot be altered
      return new ArrayList(list);
    }
    

    What is the advantage of immutability?

    The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.

    By having an immutable object, one can ensure that all threads that are looking at the object will be seeing the same state, as the state of an immutable object will not change.

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

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • 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 PLEASE make sure you read up on SQL injections before… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer If you let jQuery manange your handlers, you could do… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer Add the following line to your routing table: routes.MapRoute("RouteName", "Help/{Thing}/{OtherThing}",… May 16, 2026 at 12: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

Related Questions

I have a java class that sets an servlet attribute to a HashMap object:
I wonder if this is possible with ant and java : I have a
I am not clear about this. When I run a java App or run
It is good practice to use the main method to test a java/.net class?
I'm sure this is incredibly common with as OOP centered as Java is. In
In my Java application i need to compare two list's element whether it is
I need to return/update a boolean while returning a list of stuff from a
I have similar problem to one discussed here , but with stronger practical usage.
If i was to make a new program and wanted to have a fraction
I have a problem with overriding the equals method in an Enum to make

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.