Making a class immutable
Immutability must be familiar to every one when we talk about String & StringBuffer classes in java. Strings are considered immutable because the values contained in the reference variable cannot be changed. Whereas String Buffer is considered mutable because the value in a string buffer can be changed (i.e. mutable).
However I always thought how to make our user defined classes as immutable though I am unaware as to why any one would need this.
The reason perhaps might be clear once we have a look at the code.
Now in order to make a class immutable we must restrict changing the state of the class object by any means. This in turn means avoiding an assignment to a variable. We can achieve this through a final modifier. To further restrict the access we can use a private access modifier. Above do not provide any method where we modify the instance variables.
Still done? No. How if some body creates a sub class from our up till now immutable class? Yes here lies the problem. The new subclass can contain methods, which over ride our base class (immutable class) methods. Here he can change the variable values.
Hence make the methods in the class also final. Or a better approach. Make the immutable class itself final. Hence cannot make any sub classes, so no question of over ridding.
The following code gives a way to make the class immutable.
Code Developed By Ashish Agarwal
This code demonstrates the way to make a class immutable
// The immutable class which is made final
final class MyImmutableClass
// instance var are made private & final to restrict the access
private final int count;
private final double value;
// Constructor where we can provide the constant value
public MyImmutableClass(int paramCount,double paramValue)
count = paramCount;
value = paramValue;
// provide only methods which return the instance var
// & not change the values
public int getCount()
public double getValue()
// class TestImmutable
public class TestImmutable
public static void main(String args)
MyImmutableClass obj1 = new MyImmutableClass(3,5);
// there is no way to change the values of count &
// no method to call besides getXX, no subclassing, no public access to var -> Immutable
The possible use of immutable classes would be a class containing a
price list represented for a set of products.
Otherwise also this represents a good design.
Have Java Problem
Do you have a Java Question?
Java Certification, Programming, JavaBean and Object Oriented Reference Books
Java Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity. Information used on this site is at your own risk.
The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.