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 class has an int field, integer. It has a constructor using its field.
  • It has a method, Mutable add(int a) that returns the instance of the Mutable object after increasing its int field to a given int value.
  • It has a method, Mutable multiply(int a) that returns the instance of the Mutable object after multipling its int field to a given int value. It also has a method ImutableObjectsExmp makeImmutable2(),that returns a new ImutableObjectsExmp with a given int field.
  • ImutableObjectsExmp class a private int field, integer. It has a constructor using its field, an int read() method that returns the value of its field, a boolean 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 method ImutableObjectsExmp multiply(int x) that both return a new ImutableObjectsExmp, 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 new Mutable object with a given int field.
  • The class has a method static ImutableObjectsExmp modify1(ImutableObjectsExmp y) that uses the add(int x) method to a given ImutableObjectsExmp and creates a new object. Then it uses multiply(int x) and add(int x) method to the new object and returns it.
  • The class finally has a method static ImutableObjectsExmp modify2(ImutableObjectsExmp y), that uses a ImutableObjectsExmp, and uses makeMutable() method to get a Mutable object. Then it uses add(int a) and multiply(int a) methods of Mutable to change its fields and then uses makeImmutable() method to return a new ImutableObjectExmp object.
  • We create a new ImutableObjectsExmp and call modify1(ImutableObjectsExmp y) to return a new ImutableObjectsExmp object.Then we call modify2(ImutableObjectsExmp y) method to get another ImutableObjectsExmp object. We print out the three objects. Both methods have changed the fields of the immutable object.

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.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button