Any one please give the diff between Mutable objects and Immutable objects with example.
-
4`String` class is a great example of an immutable object. – Qwerky Jan 11 '11 at 14:10
-
See also [Mutable vs immutable objects](http://stackoverflow.com/questions/214714/mutable-vs-immutable-objects) (not Java-specific) – Péter Török Jan 11 '11 at 14:15
-
btw, take a look at [`com.jcabi.aspects.Immutable`](http://www.jcabi.com/jcabi-aspects/annotation-immutable.html) annotation – yegor256 Feb 18 '13 at 07:06
5 Answers
Mutable objects have fields that can be changed, immutable objects have no fields that can be changed after the object is created.
A very simple immutable object is a object without any field. (For example a simple Comparator Implementation).
class Mutable{
private int value;
public Mutable(int value) {
this.value = value;
}
//getter and setter for value
}
class Immutable {
private final int value;
public Immutable(int value) {
this.value = value;
}
//only getter
}

- 26,627
- 26
- 120
- 132

- 118,862
- 56
- 287
- 383
Mutable objects can have their fields changed after construction. Immutable objects cannot.
public class MutableClass {
private int value;
public MutableClass(int aValue) {
value = aValue;
}
public void setValue(int aValue) {
value = aValue;
}
public getValue() {
return value;
}
}
public class ImmutableClass {
private final int value;
// changed the constructor to say Immutable instead of mutable
public ImmutableClass (final int aValue) {
//The value is set. Now, and forever.
value = aValue;
}
public final getValue() {
return value;
}
}

- 1,506
- 3
- 32
- 50

- 19,267
- 11
- 56
- 72
Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.
For example:(Point is mutable and string immutable)
Point myPoint = new Point( 0, 0 );
System.out.println( myPoint );
myPoint.setLocation( 1.0, 0.0 );
System.out.println( myPoint );
String myString = new String( "old String" );
System.out.println( myString );
myString.replaceAll( "old", "new" );
System.out.println( myString );
The output is:
java.awt.Point[0.0, 0.0]
java.awt.Point[1.0, 0.0]
old String
old String
-
-
-
myString.replaceAll( "old", "new" ); -- This code must be like that myString=myString.replaceAll( "old", "new" ); -- I think you have used it in wrong way . – Tugrul Dec 12 '13 at 15:28
-
5@TugrulAsik - Ma friend, he used that in correct way only. replace/replaceAll methods will return the value. If u want that store to a string and use it. Either u can store to same string or create a new string object and use it. – Arundev Apr 29 '14 at 17:59
-
What exactly happened when `myString.replaceAll( "old", "new" );`. Did that do nothing or did it create a new string with value "new String" but since we did not store it, it got lost. What happened? – ARK Jan 08 '20 at 08:32
They are not different from the point of view of JVM. Immutable objects don't have methods that can change the instance variables. And the instance variables are private; therefore you can't change it after you create it. A famous example would be String. You don't have methods like setString, or setCharAt. And s1 = s1 + "w" will create a new string, with the original one abandoned. That's my understanding.

- 2,128
- 2
- 23
- 36
-
2... and all instance variables are private! (otherwise -> mutable) – Andreas Dolk Jan 11 '11 at 14:17
-
1
-
1