- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between mutable and immutable object
In Java, state of the immutable object can’t be modified after it is created b ut definitely reference other objects. They are very useful in multithreading environment because multiple threads can’t change the state of the object so immutable objects are thread safe. Immutable objects are very helpful to avoid temporal coupling and always have failure atomicity.
On the other hand, Mutable objects have fields that can be changed, immutable objects have no fields that can be changed after the object is created.
Sr. No. | Key | Mutable object | Immutable object |
---|---|---|---|
1 | Basic | We can modify the state of a mutable object after it is created | We can't modify the state of the object after it is created. |
2 | Thread safe | Mutable objects are not thread safe | Immutable objects are thread safe. |
3 | Final | Mutable class are not final | To create an immutable object, make the class final |
4 | Example | By default all class and it's object are mutable by nature. | String and all wrapper class are the example of immutable classes |
Example of Immutable Class
public final class ImmutableClass { private String laptop; public String getLaptop() { return laptop; } public ImmutableClass(String laptop) { super(); this.laptop = laptop; } } public class Main { public static void main(String args[]) { ImmutableClass immutableClass = new ImmutableClass("Dell"); System.out.println(immutableClass.getLaptop()); } }
Example of Muttable Class
public class MuttableClass { private String laptop; public String getLaptop() { return laptop; } public void setLaptop(String laptop) { this.laptop = laptop; } public MuttableClass(String laptop) { super(); this.laptop = laptop; } } public class Main { public static void main(String args[]) { MuttableClass muttableClass = new MuttableClass("Dell"); System.out.println(muttableClass.getLaptop()); muttableClass.setLaptop("IBM"); System.out.println(muttableClass.getLaptop()); } }
- Related Articles
- Difference between mutable and immutable in python?
- What is the difference between a mutable and immutable string in C#?
- Name mutable and immutable objects in Python
- How to create an immutable class with mutable object references in Java?\n
- What does immutable mean? Which Python types are mutable and which are not?
- Difference between Object and Class in Java
- Difference Between Object and Class in C++
- How to make object properties immutable in TypeScript?
- What is the difference between Object oriented programming and Object based programming?
- Difference between a "class" and an "object" in Kotlin
- What is the difference between `new Object()` and object literal notation in JavaScript?
- What is the difference between object and reference in java?
- What is the difference between a String object and a StringBuffer object in java?
- C++ mutable keyword?
- Difference between Object level lock and Class level lock in Java

Advertisements