Are private methods really safe?

后端 未结 7 1544
旧巷少年郎
旧巷少年郎 2021-01-30 15:26

In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn\'t know about that method either.

7条回答
  •  有刺的猬
    2021-01-30 16:00

    The question is who are you trying to save it from. In my opinion, such a client of your code is the one at a loss here.

    Any piece of code (written by you or others) which tries to access a private member of the above class is essentially digging its own grave. private members don't make a part of the public API and are subject to change without notice. If a client happens to consume one of such private members in the manner given above, it's going to break if it upgrades to a newer version of the API in which the private member got modified.

提交回复
热议问题