Immutable Class Getter with Defensive Copies

The immutable class has the property that once the object is created it cannot be updated. The immutable class getter method is used to get the current values of data members, but if the data member has an object reference to some other class, it can be used to modify the immutable class data member later as shown below.

package org.wesome.dsalgo;

import java.util.Date;
import java.util.Objects;

public class Apple {
    String name;
    float price;
    Date date;

    public Apple() {
    }

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

    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;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @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) && Objects.equals(date, apple.date);
    }

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

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

import org.junit.jupiter.api.Test;

import java.util.Date;

public class AppleTest {
    @Test
    public void finalFieldTest() {
        Date date = new Date();
        Apple apple = new Apple("Mcintosh", 1.1f, date);
        System.out.println(apple);
        apple.getDate().setYear(100);
        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 Getter have Defensive Copies

To make the class immutable, the getter will create a new object from the value and then assign it to a class member, so that the object reference cannot be modified later.

package org.wesome.dsalgo;

import java.util.Date;
import java.util.Objects;

public class Apple {
    String name;
    float price;
    Date date;

    public Apple() {
    }

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

    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;
    }

    public Date getDate() {
        return new Date(this.date.getYear(), this.date.getMonth(), this.date.getDate(), this.date.getHours(), this.date.getMinutes(), this.date.getSeconds());
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @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) && Objects.equals(date, apple.date);
    }

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

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

import org.junit.jupiter.api.Test;

import java.util.Date;

public class AppleTest {
    @Test
    public void finalFieldTest() {
        Date date = new Date();
        Apple apple = new Apple("Mcintosh", 1.1f, date);
        System.out.println(apple);
        apple.getDate().setYear(100);
        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