The immutable class has the property that once the object is created it cannot be updated. Immutable class constructor is used to initialising and assign values to data members only once so that they cannot be modified later.
values passed in Constructor can have an object reference which can be updated 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);
date.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 Constructor have Defensive Copies
To make the class immutable, the constructor 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 = new Date(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
}
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);
date.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()
}