Software Engineer : Immutable Class in Java Using Builder Design Pattern

Shrirang Pinjarkar
3 min readFeb 3, 2021

Hello guys, lets have some fun. I have been fan of Java and many times I do something string manupulation or specifically if you try to change char at some index in string, it will give you error.

String is immutable in java.

This error gave me motivation what is exactly immutable class is and how it is implemented. So I did read some articles available and look at the code how string class in java is implemented (Link).

After doing some research, I understood how immutable class are implemented using builder pattern.

Now lets implement it so that we can understand both builder design pattern and immutable class.

First, you have to create your immutable class. Following are the rules.

  • All the variables of the immutable class must be private and they have only getter methods.
  • It should have only private constructor with Builder object as parameter that will be used to create the immutable class.
  • Variables of type array, hashmap, etc must be copied or cloned to avoid modification.

Second, now you have to create static builder class inside immutable class, that will create object of immutable class and return it.

  • Same variables as immutable class must be created in builder class and all should be private.
  • Create public setter method for all variables and will return the this object.
  • Create build method that will create instance of immutable class and return it.

Now look and understand the code.

Immutable Class

package models;import java.util.Arrays;public class ImmutableClass {
private Integer integer;private float decimal;private String word;private int[] array;
public Integer getInteger() {return integer;}public float getDecimal() {return decimal;}public String getWord() {return word;}public int[] getArray() {return array;}private ImmutableClass(ImmutableClassBuilder builder){this.integer = builder.integer;this.decimal = builder.decimal;this.word = builder.word;this.array = Arrays.copyOf(builder.array, builder.array.length);}public static class ImmutableClassBuilder{private int integer;private float decimal;private String word;private int[] array;public ImmutableClassBuilder setInteger(int integer) {this.integer = integer;return this;}public ImmutableClassBuilder setDecimal(float decimal) {this.decimal = decimal;return this;}public ImmutableClassBuilder setWord(String word) {this.word = word;return this;}public ImmutableClassBuilder setArray(int[] array) {this.array = array;return this;}public ImmutableClass build(){return new ImmutableClass(this);}}}

So, what is the logic behind using builder pattern?

Now understand how we can create object of Immutable class.

DemoBuilderPattern Class

import models.ImmutableClass;public class DemoBuilderPattern {public static void main(String[] args) {int arr[] = new int[] {1,2,3};ImmutableClass immutableClass1 = new ImmutableClass.ImmutableClassBuilder().setInteger(120).setDecimal(1.2f).setWord("word").setArray(arr).build();ImmutableClass immutableClass2 = new ImmutableClass.ImmutableClassBuilder().setInteger(120).setDecimal(1.2f).setWord("word").setArray(arr).build();System.out.println("ImmutableClass1 : "+immutableClass1);System.out.println("ImmutableClass2 : "+immutableClass2);System.out.println(""+immutableClass1.getArray().hashCode());System.out.println(""+immutableClass2.getArray().hashCode());}}

Builder class is static class inside immutable class. It can be called by using class name of immutable class. Now it will call setter methods that will initialize variables of builder class. After all variables are initialize, it will call build method to create instance of Immutable class and return it.

This is it! So, simple!

Kill one bird with one stone.

Immutable Class and Builder Design Pattern

Connect me on LinkedIn. If you have some interesting project, we can collab on github.

--

--

Shrirang Pinjarkar

An Independent Self Motivated Programmer who loves to solve technical and logical challenges. Love Data Structure & Algorithms Problems, Mathematics.