Immutable Class Without Setters

Setter methods are used to update the data fields of an object, but the immutable class has the property that once the object is created it cannot be updated. If an immutable class has setter methods, its data members can be updated later as shown below.

package org.wesome.dsalgo;

import java.util.Objects;

public class Apple {
    String name;
    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);
        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 Don't have Setter Method

To make the class immutable, setter methods should be removed so that once an object is created, it cannot be modified later.

package org.wesome.dsalgo;

import java.util.Objects;

public class Apple {
    String name;
    float price;

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

    public String getName() {
        return name;
    }
    /*  immutable class don't have setter method */
    /*public void setName(String name) {
        this.name = name;
    }*/

    public float getPrice() {
        return price;
    }
    /*  immutable class don't have setter method */
   /* 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 don't have 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