Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Immutable class in Java | How to create Immutable class in Java

  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • Aranya Kand with Hindi Meaning | अरण्यकाण्ड | सीता हरण
    Aranya Kand with Hindi Meaning | अरण्यकाण्ड का अर्थ | सीता हरण Spiritual
  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • Pythagorean Theorem in Hindi, Definition
    Pythagorean Theorem in Hindi, Definition, Formula, Proof पाइथागोरस थ्योरम क्या है जानिए हिंदी में? Science
  • Indian Navy Day
    Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है? General Knowledge
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography

Immutable class in Java | How to create Immutable class in Java

Posted on August 14, 2021February 7, 2022 By GeekCer Education No Comments on Immutable class in Java | How to create Immutable class in Java
Immutable class in Java

Immutable class in java means that once we create an object of class we cannot change the content of that object. If someone is trying to change the contents a new object will be created.

Immutable objects are mainly useful in concurrent applications. They cannot be corrupted by thread intervention or seen in an inconsistent state because they cannot change state.

What does immutability mean? Immutability means unable to change or sensitive to change. It will be easier for you to do concurrent programming if you use immutable classes. Immutable classes ensure that the value is not changed in the middle, even if you don’t use synchronized blocks.

Table of Contents

  • Simple Steps to Create Immutable Class in Java
  • What are the advantages of Immutable Class?
  • What are the disadvantages of Immutable object?
  • Examples of the Immutable Class
  • FAQ

Simple Steps to Create Immutable Class in Java

These are the simple Steps to Create custom Immutable class in Java. You have to follow the following steps to create an immutable class:

  • Declare your class final, so that no other classes can extend it.
  • Don’t provide setter methods for the fields/variables.
  • Make all fields/variables final and private.
  • Initialize all the fields via using a parameterized constructor.

This the sample form of custom immutable class where Example is the name of the class.

                  public final class Example {
   // Variable with private and final modifiers.
  private final int variable;

   // Parameterized constructor 
  public Example(int variable) {
    this.variable= variable;
  }

  public Example change(int variable) {
    if (this.variable == variable) {
      //Check the value of variable if it is equal to your provided value. 
       //If both are same then the content does not change, just return the current object.
      return this;
    } else {
       //If the content changes, create a new object with the updated content.
      return new Example(variable);
    }
  }
}

The class in which we will create main method

                  public class Main {
  public static void main(String[] args) {
    Example obj1 = new Example(10);
    Example obj2 = obj1.change(10);
    Example obj3 = obj1.change(20);

    System.out.println(obj1 == obj2);
    System.out.println(obj1 == obj3);
  }
}

Output:
true
false

What are the advantages of Immutable Class?

There are following advantages of immutable classes or objects:

  1. Immutable classes are easy to create, test and use and such classes are automatically thread-safe and do not have synchronization issues.
  2. By using immutable class we don’t need copy constructor.
  3. The state of an immutable object will not change, so in the multiple places we can use the same object hence we can save a lot of memory.

What are the disadvantages of Immutable object?

Immutable objects are just like use and throw because we cannot reuse it. This type of object can generate a lot of garbage and potentially slow down the application so this is the primary disadvantage of immutable object.

Examples of the Immutable Class

The String class in Java is a good example of an immutable class. Other than String, the wrapper classes for the primitive types like Integer, Byte, Character, Short, Boolean, Long, Double, Float are the example of immutable class.

FAQ

Are integers immutable in Java?

Yes

Is Boolean immutable Java?

Yes

Is String a wrapper class?

No. String isn’t a wrapper class because it doesn’t wrap any parallel primitive types.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Important, Java, Programs Tags:Create immutable class in java, Data Types, Disadvantages of immutable class in Java, Good example of Immutable class, How can we create immutable class?, How can you achieve immutability in your own code?, Immutable class in java example, Software Development, Synchronization, Thread, Thread Safe, Use of immutable class in Java, What is immutable design pattern?, What is the immutable class in Java?

Post navigation

Previous Post: Iterate Map in Java, Map.entrySet(), Iterators, foreach Loop
Next Post: Difference between JPanel and JFrame

More Related Articles

HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
ACID Properties in Hindi? ACID Properties क्या है? ACID Properties in Hindi? ACID Properties क्या है? Important
Java ArrayList Java ArrayList Java
Difference between Interface and Abstract class Difference between Interface and Abstract class Differences
Method Overloading in Java Method Overloading in Java Java
Operational Qualification Operational Qualification (OQ) Important

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved