Back to Java Course

Java Course

Immutable class means once an object is created, we cannot change it's content i.e.. State doesn't change.

  • It is a thread safe
  • can be cached.

In Java all the wrapper classes(like Boolean, Byte, Short etc) and String class is immutable.

Immutable class can be created by using below

  • class must be declared as final(so that child classes can't be created).
  • data members in the class must be declared as final(so that we can't change the value of it after object creation).
  • Initialize all the data members using public parameterized constructor.
  • getter methods for all variables in it.
  • no setters(to not have an option to change the value of instance variable).
  • make all fields private so that direct access is not allowed.
  • return copy of mutable instance variable rather than returning original object.

Let's see sample code which is having only immutable members.

public final class Employee{ private final String name; private final int empId; public Employee(String name, int empId) { this.name=name; this.empId=empId; } public String getName() { return name; } public int getEmpId() { return empId; } }

Let's see sample code which is having both immutable and mutable data members.

public final class CustomImmutable { private final String sName; private final StringBuffer sbName; public CustomImmutable(String sName, StringBuffer sbName) { this.sName = sName; this.sbName = sbName; } public String getSName() { return sName; } public StringBuffer getsbName() { // return copy of mutable instance variable rather than returning original object. return new StringBuffer(sbName.toString()); } }

Let's see sample code

// Immutable class is nothing but content or state will not be changed public final class EmployeeImmutableClass { private final String empName; private final long empID; private final long empSalary; private final Address address; public EmployeeImmutableClass(String empName, long empID, long empSalary,Address address) { this.empName = empName; this.empID = empID; this.empSalary = empSalary; this.address=address; } public String getEmpName() { return empName; } public long getEmpID() { return empID; } public long getEmpSalary() { return empSalary; } public Address getAddress() throws CloneNotSupportedException { return (Address) address.clone(); } public static void main(String[] args) throws CloneNotSupportedException { EmployeeImmutableClass eic= new EmployeeImmutableClass("Praveen",149903L,1000000L,new Address(10001L,"Rangareddy","Hyderabad")); Address address=eic.getAddress(); System.out.println(address); // Address [id=10001, desc=Rangareddy, location=Hyderabad] address.setLocation("Tirupati"); address.setDesc("Railway Colony"); System.out.println(eic.getAddress()); // Address [id=10001, desc=Rangareddy, location=Hyderabad] } }
// This is an mutable class public class Address implements Cloneable{ Long id; String desc; String location; public Address(Long id, String desc, String location) { super(); this.id = id; this.desc = desc; this.location = location; } public void setId(Long id) { this.id = id; } public void setDesc(String desc) { this.desc = desc; } public void setLocation(String location) { this.location = location; } public Long getId() { return id; } public String getDesc() { return desc; } public String getLocation() { return location; } @Override public String toString() { return "Address [id=" + id + ", desc=" + desc + ", location=" + location + "]"; } public Object clone()throws CloneNotSupportedException{ return super.clone(); } }