How to create immutable class in "Java"

Immutable.....?
Immutable objects......?

Immutable objects are instance whose state does not change after it has been initialized.
For example, one of our favourite class String is also immutable, that's why once it's instantiated its value never changed.
And all other wrapper classes(Integer, Boolean, Byte, Short) all are Immutable class.

There is some following way, by using that we can also make our own Immutable class.


  1. Declare the class as final so it can't be extended.
  2. Make all fields private so that direct access is not allowed.
  3. Don't provide setter methods for the variables.
  4. Make all Mutable fields final, so that its values can be assigned only once.
  5. Initialize all the fields via constructor performing deep copy.
  6. Performing cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

9 comments:

  1. It's really a piece of new information to me, I learn more from this blog, I want to learn more from this blog, keep on it doing, I eagerly waiting for your updates, thank you!!

    java training institute in delhi

    ReplyDelete
    Replies
    1. good to see your optimistic thought on this.
      Thanks a bunch.

      Delete
  2. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. Further More Information About JAVA Training Institute in Delhi So Contact Here-+91-9311002620 Or Visit Website- https://htsindia.com/java-training-courses

    ReplyDelete
    Replies
    1. its pleasure to serve all the users a good quality content, i'll be continue.....

      Delete
  3. Just imagine, what if you asked to write a Java assignment, but you don't know how to do it? Well, don't worry because you can Buy Assignment Writing Services online like many other students do.

    ReplyDelete
  4. Java is one of the mostly widely used programming language in enterprise software development. I appreciate your efforts in writing these educational blogs, thank you for sharing this. Best Java Training Institute In Chennai With Placement

    ReplyDelete
  5. This information is very good; I go through your every blog. They are very knowledgeable. Thanks for sharing this information. If anyone want to get experience in Gurgaon can contact me at - 9599119376 or can visit our website at
    Experience Certificate In Gurgaon
    Experience Certificate In Delhi
    Experience Certificate In Noida

    ReplyDelete

  6. Thank you for sharing this valuable information with us. Keep writing this type of the knowledgeable blogs. If anyone is interested for taking experience certificate in Delhi.
    Dreamsoft is the 20years old consultancy providing experience certificate in Delhi for your experience certificate in Delhi you can contact at – 9599119376 or visit our website at https://experiencecertificates.com/experience-certificate-provider-in-delhi.html

    ReplyDelete