'Mutable-immutable class pattern on Java depending on package
Is there any pattern in java that makes a class mutable inside a package and immutable outside of it?
Solution 1:[1]
Simply refrain from using any access modifier on all the fields and on those methods that modify the fields, e.g. String secret; or void setSecret(String secret) { ... }. A field/method that does not have an access modifier is package-private, which means that it can only be seen by code that resides in the same package.
However, as @JoachimSauer and @Darkman point out, such a class is not in fact immutable. Immutable means that an instance will never change (no matter by whom) after it has been constructed. The class will simply be encapsulated: only a limited part of your codebase (the package where the class is defined) may modify instances of the class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
