C++-like friend class mechanism in Java

前端 未结 4 1678
天涯浪人
天涯浪人 2021-01-12 21:40

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

4条回答
  •  鱼传尺愫
    2021-01-12 22:15

    In Java, you would create a group of "friends" by putting multiple classes in the same package. Then, use default scope (no access specifier) on the classes, methods and fields you want to restrict access to.

提交回复
热议问题