Home >Java >javaTutorial >Immutable Class in Java
In this article, we will explain the immutable class in java. Also, we will see the uses of immutable class. There will be java code examples showing how immutable class can be created in java. An immutable class is a class whose contents cannot be changed.
Following are some important points regarding the immutable class:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Before using this class, it is important to know how an immutable class is created. Following are important points to be kept in mind while creating the class:
Following are the main uses of immutable classes in java:
Apart from the listed advantages, there can be some performance issues when using immutable classes. Hence where thread safety and other features are not important, we can consider using mutable classes for performance improvement.
Below are the examples mentioned:
Now we will see java code examples showing the creation of immutable classes in java. Here is a java code example showing how immutable classes are created in java:
Code:
package com.edubca.immutabledemo; public final class ImmutableDemo { final String studentName; final int rollNumber; public ImmutableDemo (String studentName, int rollNumber) { this.studentName = studentName; this.rollNumber = rollNumber; } public String getStudentName() { return studentName; } public int getRollNumber() { return rollNumber; } public static void main(String[] args) { ImmutableDemo obj = new ImmutableDemo ("John" , 100); // Since no getters are available contents cannot be modified. // Also as variables are declared final they cannot be modified System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); } }
Output:
The above code shows the creation of an immutable class that does not contain any mutable object as a class member.
Now we will see how to create immutable classes having mutable objects as their members. Some special handling is required In order to maintain immutability in this case.
Problem with a Mutable Object in an Immutable Class:
Here is code example normal showing creation of immutable class having mutable members in it:
Code:
package com.edubca.immutabledemo; import java.util.Date; import java.text.SimpleDateFormat; public final class ImmutableDemo { final String studentName; final int rollNumber; final Date birthDate; public ImmutableDemo (String studentName, int rollNumber, Date birthDate) { this.studentName = studentName; this.rollNumber = rollNumber; this.birthDate=birthDate; } public String getStudentName() { return studentName; } public int getRollNumber() { return rollNumber; } public Date getBirthDate() { return birthDate; } public static void main(String[] args) throws Exception { String birthDate= "31/09/1997"; ImmutableDemo obj = new ImmutableDemo ("John" , 100, new SimpleDateFormat("dd/MM/yyyy").parse(birthDate)); System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); System.out.println("Birth date is " + obj.getBirthDate()); obj.getBirthDate().setTime(1000); System.out.println("After changing birth date>>>>>>>>>>>>"); System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); System.out.println("Birth date is " + obj.getBirthDate()); } }
Output:
From the above output, we can see that the contents of the object are changed since the birth date is changed. This breaks the rules of the immutable class.
The Solution to the Problem with a Mutable Object in an Immutable Class:
In order to handle such cases, some changes need to be implemented in the code. In the modified code, when returning mutable objects from the getting method, we do not return the original object; rather, we return the clone of the object. Hence changes to cloned objects do not have any effect on the original object. Here is the modified code:
Code:
package com.edubca.immutabledemo; import java.util.Date; import java.text.SimpleDateFormat; public final class ImmutableDemo { final String studentName; final int rollNumber; final Date birthDate; public ImmutableDemo (String studentName, int rollNumber, Date birthDate) { this.studentName = studentName; this.rollNumber = rollNumber; this.birthDate=birthDate; } public String getStudentName() { return studentName; } public int getRollNumber() { return rollNumber; } public Date getBirthDate() { return (Date)birthDate.clone(); } public static void main(String[] args) throws Exception { String birthDate= "31/09/1997"; ImmutableDemo obj = new ImmutableDemo ("John" , 100, new SimpleDateFormat("dd/MM/yyyy").parse(birthDate)); System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); System.out.println("Birth date is " + obj.getBirthDate()); obj.getBirthDate().setTime(1000); System.out.println("After changing birth date>>>>>>>>>>>>"); System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); System.out.println("Birth date is " + obj.getBirthDate()); } }
Output:
From the above output, we can see no change in date value; hence, the class’s immutability remains intact.
From the above discussion, we have a clear understanding of java immutable classes. Also, we have seen the advantages of it.
The above is the detailed content of Immutable Class in Java. For more information, please follow other related articles on the PHP Chinese website!