Do you know how can I make an object changeable only inside a special class? In this example I want the object PrivateObject to be only changable (incrementable) inside the
Java does not have any language feature equivalent of C++'s friend keyword. However , there are number of application-level alternatives :
increment()
method of the PrivateObject
class as package- private and define Box in the same package.OR
2.Let the calling code pass a token into increment()
- inside this method check if this token is indeed coming from Box
class and allow or disallow.
The idea is to keep the coupling between PrivateObject
and Box
class manageable.