Provide Best Programming Tutorials

Immutable Class In Java

Immutable Objects and Classes

You can define immutable classes to create immutable objects. The contents of immutable objects cannot be changed.

Normally, you create an object and allow its contents to be changed later. However, occasionally it is desirable to create an object whose contents cannot be changed once the object has been created. We call such an object as immutable object and its class as immutable class. The String class, for example, is immutable.

For a class to be immutable, it must meet the following requirements:

  • All data fields must be private.

  • There can’t be any mutator methods for data fields.

  • No accessor methods can return a reference to a data field that is mutable.


  1. If a class contains only private data fields and no setter methods, is the class immutable?

  1. If all the data fields in a class are private and of primitive

Leave a Reply

Close Menu