5

As the title says , Why are the instance variables are declared as private in immutable pattern ? I think that the keyword final is enough to avoid any changes that can be done to the value of the instance variable. So why do we also use private keyword with it ?

12
  • 2
    What happens if I'm programming in a language that doesn't have the final keyword? Apr 22, 2019 at 20:34
  • 1
    immutability isn't a pattern really... What sort of thing are you thinking of/referring to?
    – Telastyn
    Apr 22, 2019 at 20:37
  • 2
    Because accessibility and mutability are two different things. Apr 23, 2019 at 2:11
  • 1
    @Telastyn In the same sense that all patterns are fundamental properties of things. Which is to say, they aren't. You might as well say that singleton-ness is a fundamental property of a class, and therefore not actually an anti-pattern.
    – user253751
    Apr 23, 2019 at 4:04
  • 1
    @immibis - Patterns are not at all fundamental, let alone properties of things.
    – Telastyn
    Apr 23, 2019 at 4:48

1 Answer 1

7

An object has the potential for (at least) two aspects:

  • a set of members intended for consumption by an external consumer, a public face, and,
  • implementation details many of which are hidden or restricted in various ways

Private is used to label members that are emphatically not ones that a consuming client is allowed to interact with it — such members are encapsulated within the broader object.

The ability to restrict the ways external code interacts with an abstraction is a key helper in providing guarantees useful in maintenance — as someone maintaining the code, we can rest assured that no one (else) in the whole project is (capable of directly) consuming those private members, so we'll never find any references to them from other code.

Final/readonly is an orthogonal concept — allowing readonly access to your instance variables makes them part of the object's client-consumable members.

For example, we can delete a private instance variable, or change its type, and know for certain that we only have to work with that one class — we will not have to worry or reason past the class itself.  Whereas if a final public member is deleted or its type changed, we will have to concern ourselves with consuming clients (meaning they will have to be modified and as they are possibly other things may also end up requiring changes).

This can be of varying severity, ranging from not to big a deal for a small program whose source code always versioned together, built, and tested — to probably most critical when a library doesn't have access to the client code that is consuming it, especially when a new version of libraries can be installed/upgraded on a user's machine without any build & test event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.