Immutable Class With Final Data Members

The immutable class has the property that once the object is created it cannot be updated. If a class has non-final data members it can be updated after the object is created as shown below.

package org.wesome.dsalgo;

import java.util.Objects;

public class Apple {
    String name;
    float price;

    public Apple() {
    }

    public Apple(String name, float price) {
        super();
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Apple apple = (Apple) o;
        return Float.compare(apple.price, price) == 0 && Objects.equals(name, apple.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

public class AppleTest {
    @Test
    public void setterMethodTest() {
        Apple apple = new Apple("Mcintosh", 1.1f);
        System.out.println(apple);
        apple.setName("Fuji");
        apple.setPrice(2.2f);
        System.out.println(apple);
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

Immutable class has Final Data Members

To make the class immutable, all data members must be final so that once the value is assigned to them, it cannot be modified later.

final data members cannot be updated via setter, it will throw compilation error

package org.wesome.dsalgo;

import java.util.Objects;

public class Apple {
    final String name;
    final float price;

    public Apple(String name, float price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Apple apple = (Apple) o;
        return Float.compare(apple.price, price) == 0 && Objects.equals(name, apple.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

public class AppleTest {
    @Test
    public void setterMethodTest() {
        Apple apple = new Apple("Mcintosh", 1.1f);
        System.out.println(apple);
        /*  immutable class final data members cannot be updated via setter method */
        apple.setName("Fuji");
        apple.setPrice(2.2f);
        System.out.println(apple);
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on