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.

10 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. Great Work. I always use to follow your valuable contents. Thanks for sharing this kind of debt knowledge.

    Java Training Institute in Delhi

    ReplyDelete
  3. 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
  4. I have gone through your post and i found it very helpful. Please do share such kind of interesting post. Otherwise if any One Want Java Training with 30% off So Contact Here-9311002620

    Job Oriented Complete JAVA Training Course

    ReplyDelete
  5. 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
  6. Anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in hyderabad, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

    ReplyDelete
  7. 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