

How to Create Custom Immutable Class with mutable object references in Java?
source link: http://adnjavainterview.blogspot.com/2019/06/how-to-create-custom-immutable-class-with-mutable-object-reference-injava.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Create Custom Immutable Class with mutable object references in Java?
In the below example, Employee class is immutable but filed Address is mutable, it's another java class .
Employee.java,
package com.practice; public final class Employee { private final String empName; private final int age; private final Address address; public Employee(String name, int age, Address address) { super(); this.empName = name; this.age = age; this.address = address; } public String getEmpName() { return empName; } public int getAge() { return age; } public Address getAddress() { return address; } }
Address.java,
package com.practice; public class Address implements Cloneable{ public String addressType; public String address; public String city; public Address(String addressType, String address, String city) { super(); this.addressType = addressType; this.address = address; this.city = city; } public String getAddressType() { return addressType; } public void setAddressType(String addressType) { this.addressType = addressType; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCity() { return city; } public void setCity(String city) { this.city = city; }
public Object clone() throws CloneNotSupportedException { return super.clone(); }
@Override public String toString() { return "Address Type - "+addressType+", address - "+address+", city - "+city; }
}
MainClass.java,
package com.practice; public class MainClass { public static void main(String[] args) { Employee emp = new Employee("Mahesh", 34, new Address("Home", "JP Nagar", "Bangalore")); Address address = emp.getAddress(); System.out.println(address); address.setAddress("Jayanagar"); address.setAddressType("Office"); address.setCity("Sangli"); System.out.println(emp.getAddress()); } }
Output:-
Address Type - Home, address - JP Nagar, city - Bangalore
Address Type - Office, address - Jayanagar, city - Sangli
Solution : -
The best solution is to modify the getAddress method in employee class, and instead of returning original Address object, we will return deep cloned copy of that instance. But one condition Address class must implements cloneable interface.
Modified employee class is as follow,
Employee.java,
package com.practice; public final class Employee { private final String empName; private final int age; private final Address address; public Employee(String name, int age, Address address) { super(); this.empName = name; this.age = age; this.address = address; } public String getEmpName() { return empName; } public int getAge() { return age; } public Address getAddress() throws CloneNotSupportedException { return (Address) address.clone(); } }
MainClass.java,
package com.practice; public class MainClass { public static void main(String[] args) { Employee emp = new Employee("Mahesh", 34, new Address("Home", "JP Nagar", "Bangalore")); Address address = emp.getAddress(); System.out.println(address); address.setAddress("Jayanagar"); address.setAddressType("Office"); address.setCity("Sangli"); System.out.println(emp.getAddress()); } }
Recommend
-
99
README.md react-copy-write
-
11
Iterators yielding mutable references, take 2 Oct 24, 2013 OK, after writing the post on iterators that y...
-
9
Python: mutable vs immutable Hi, I'm Ana. I'm a software developer. I mostly do Python. This blog is about my adventures...
-
13
Collaborator rust-log-analyzer commented
-
13
Collaborator rust-highfive
-
20
When immutable objects can not be used in Java or is it better to use mutable? advertisements It is obvious that immutable objects should be u...
-
5
New issue Suggest both of immutable and mutable trait implementations #89263
-
12
The third article of the Python vs C++ Series is about immutability – an object cannot be modified after it is created. (Not...
-
14
Conversation Copy link Member
-
9
Python's Mutable vs Immutable Types: What's the Difference? Remove ads As a Python develop...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK