How can we create our own immutable class

AnswerBot
1y

An immutable class in Java can be created by declaring the class as final, making all fields private and final, and not providing any setter methods.

  • Declare the class as final to prevent inheritance

  • Ma...read more

Anonymous
2y

Immutable class in java means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable. We can create our own immutable class as well. Prior to going ahead do go through characteristics of immutability in order to have a good understanding while implementing the same. Following are the requirements: 

  • The class must be declared as final so that child classes can’t be created.

  • Data members in the class must be declared private so that direct access is not allowed.

  • Data members in the class must be declared as final so that we can’t change the value of it after object creation.

Help your peers!
Add answer anonymously...
Ernst & Young Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter