Immutable Class With Private Data Members

The immutable class has the property that once the object is created it cannot be updated, but if the data member is publically accessible, it can be modified as well 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.name = "Fuji";
        apple.price = 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 Private Data Members

To make the class immutable, data members should have a private access modifier, so that once an object is created, it cannot be modified later.

package org.wesome.dsalgo;

import java.util.Objects;

public class Apple {
    /*  immutable class must have private access modifier */
    private String name;
    /*  immutable class must have private access modifier */
    private 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 + '}';
    }
}

private data members cannot be accessed out side the class, this will throw compilation error

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.name = "Fuji";
        apple.price = 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