DRAFT
PrivateFieldsImplyImmutability
Misconception:
If all instance variables of a class are private, then nobody from outside can change the state of the object, and thus the object is immutable.
Incorrect
A class where all fields are private is immutable
Correct
A class where all fields are private can still be mutable
CorrectionHere is what's right.
Here is what's right.
While it is correct that code in other classes cannot touch private fields in our class, the values of private
fields in our class can be changed by methods of that class (e.g., by a setter method).
Thus, there is a way for the fields to change their value, and thus the class is mutable.
Language
Java