1.

What Is Immutable Class And How To Create An Immutable Class In Java ?

Answer»

An immutable class is one in which once an object is created, then we cannot alter the values, or we cannot change its content. As in Java, we have wrapper CLASSES like String, Boolean, Integer, etc., all these classes are immutable classes, and we can ALSO create an immutable class by own.

Following are some REQUIREMENT to create an immutable class in Java:

  • A class needs to DECLARE as final so that it cannot be inherited.
  • All the data MEMBERS of the class need to declare as final so its value can't be changed.
  • No option for setters method.
  • There should be getter methods for all variables.

An immutable class is one in which once an object is created, then we cannot alter the values, or we cannot change its content. As in Java, we have wrapper classes like String, Boolean, Integer, etc., all these classes are immutable classes, and we can also create an immutable class by own.

Following are some requirement to create an immutable class in Java:



Discussion

No Comment Found