Create an Immutable Class in Java & How Can handle Mutable Objects in Immutable Class with Example
How to Create an Immutable Class in Java
How Do We Create an Immutable Class ??
follow the below steps:1-Make your class final, so that no other classes can extend it.
2-Make all your fields final, so that they’re initialized only once inside the constructor
3-Don’t expose setter methods. only write getter method
Simple Example of Immutable
public final class Employee {
private final int id;
private final String name;
public Employee(int id, String name) {
this.name = name;
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
Important Question
How Can handle Mutable Objects in Immutable Class
Like If I Have a Employee class and Address Class .
Address present in Employee class.
Class Employee{
private String name ;
private Address address;
}
Now I want to make Employee class is Immutable Class…
follow the steps:
===============
package web.org.inmutable;
public final class Employee {
private final String name;
private final Address address;
public Employee(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
@Override
public String toString() {
return “Employee [name=” + name + “, address=” + address + “]”;
}
}
=================
public class Address {
private String city;
private String country;
private int code;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Address(String city, String country, int code) {
super();
this.city = city;
this.country = country;
this.code = code;
}
@Override
public String toString() {
return “Address [city=” + city + “, country=” + country + “, code=” + code + “]”;
}
=======================
Immutable Testing
public class InmutabilTest {
public static void main(String[] args) {
Address add = new Address(“Box”, “india”, 123);
Employee emp = new Employee(“Sanjay”, add);
System.out.println(“Testing of Object “+emp);
add.setCity(“Mirzapur”);
System.out.println(“Now I am able to change city name”+emp);
}
}
-=================================
Output
Testing of Object Employee [name=Sanjay, address=Address [city=Box, country=india, code=123]]
Now I am able to change city name
Employee [name=Sanjay, address=Address [city=Mirzapur, country=india, code=123]]
Above example we are able to modify the city of Employee So Employee is not immutable .
How Can I prevent
Only one Change in Employee constructor
public Employee(String name, Address address) {
this.name = name;
Address add = new Address();
add.setCity(address.getCity());
add.setCode(address.getCode());
add.setCountry(address.getCountry());
this.address = add;
}
See output ../……….
Testing of Object Employee [name=Sanjay, address=Address [city=Box, country=india, code=123]]
Now I am able to change city nameEmployee [name=Sanjay, address=Address [city=Box, country=india, code=123]]
I am trying to change but we can’t .
Thanks