Sobes.tech
Junior — Middle

What key features distinguish a class whose instances cannot be modified after creation?

sobes.tech AI

Answer from AI

A class whose objects cannot be modified after creation (immutable) has the following key features:

  • All class fields are declared as final (in Java), preventing reassignment.
  • Fields are either of primitive types or references to immutable objects.
  • Absence of setters or other methods that modify the object's state.
  • If the class contains references to mutable objects, it creates copies of them when passing and returning to prevent external modifications.
  • The class itself is declared as final to prevent inheritance and behavior modification.

Example: the String class in Java is immutable; its state cannot be changed after creation.