class
Modify Immutable Objects
This is an example of how to modify immutable objects. Immutable objects are objects whose state cannot change after construction. We have created two classes Mutable
and ImutableObjectsExmp
, that have the same field, but one is mutable and the other one immutable:
- Mutable
- It has a method,
Mutable add(int a)
that returns the instance of theMutable
object after increasing its int field to a given int value. - It has a method,
Mutable multiply(int a)
that returns the instance of theMutable
object after multipling its int field to a given int value. It also has a methodImutableObjectsExmp makeImmutable2()
,that returns a newImutableObjectsExmp
with a given int field. ImutableObjectsExmp
class a private int field,integer
. It has a constructor using its field, anint read()
method that returns the value of its field, aboolean nonzero()
method that returns true only if its field’s value is not zero.- The class also has a method
ImutableObjectsExmp add(int x)
and another methodImutableObjectsExmp multiply(int x)
that both return a newImutableObjectsExmp
, with its field added in the first method and multiplied in the second one with the given int. It has another method,Mutable makeMutable()
, that returns a newMutable
object with a given int field. - The class has a method
static ImutableObjectsExmp modify1(ImutableObjectsExmp y)
that uses theadd(int x)
method to a givenImutableObjectsExmp
and creates a new object. Then it usesmultiply(int x)
andadd(int x)
method to the new object and returns it. - The class finally has a method
static ImutableObjectsExmp modify2(ImutableObjectsExmp y)
, that uses aImutableObjectsExmp
, and usesmakeMutable()
method to get aMutable
object. Then it usesadd(int a)
andmultiply(int a)
methods ofMutable
to change its fields and then usesmakeImmutable()
method to return a newImutableObjectExmp
object. - We create a new
ImutableObjectsExmp
and callmodify1(ImutableObjectsExmp y)
to return a newImutableObjectsExmp
object.Then we callmodify2(ImutableObjectsExmp y)
method to get anotherImutableObjectsExmp
object. We print out the three objects. Both methods have changed the fields of the immutable object.
class has an int field, integer
. It has a constructor using its field.Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; class Mutable { private int integer; public Mutable(int initValue) { integer = initValue; } public Mutable add(int a) { integer += a; return this; } public Mutable multiply(int a) { integer *= a; return this; } public ImutableObjectsExmp makeImmutable2() { return new ImutableObjectsExmp(integer); } } public class ImutableObjectsExmp { private int integer; public static void main(String[] args) { ImutableObjectsExmp i2 = new ImutableObjectsExmp(47); ImutableObjectsExmp r1 = modify1(i2); ImutableObjectsExmp r2 = modify2(i2); System.out.println("i2 = " + i2.read()); System.out.println("r1 = " + r1.read()); System.out.println("r2 = " + r2.read()); } public ImutableObjectsExmp(int initVal) { integer = initVal; } public int read() { return integer; } public boolean nonzero() { return integer != 0; } public ImutableObjectsExmp add(int x) { return new ImutableObjectsExmp(integer + x); } public ImutableObjectsExmp multiply(int x) { return new ImutableObjectsExmp(integer * x); } public Mutable makeMutable() { return new Mutable(integer); } public static ImutableObjectsExmp modify1(ImutableObjectsExmp y) { ImutableObjectsExmp val = y.add(12); val = val.multiply(3); val = val.add(11); val = val.multiply(2); return val; } // This produces the same result: public static ImutableObjectsExmp modify2(ImutableObjectsExmp y) { Mutable m = y.makeMutable(); m.add(12).multiply(3).add(11).multiply(2); return m.makeImmutable2(); } }
Output:
i2 = 47
r1 = 376
r2 = 376
This was an example of how to modify immutable objects in Java.