Are private methods really safe?

后端 未结 7 1537
旧巷少年郎
旧巷少年郎 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条回答
  •  -上瘾入骨i
    2021-01-30 16:19

    It depends on what you mean by "safe". If you're running with a security manager that allows this sort of thing, then yes, you can do all kinds of nasty things with reflection. But then in that kind of environment the library can probably just be modified to make the method public anyway.

    Access control is effectively "advisory" in an environment like that - you're effectively trusting the code to play nicely. If you don't trust the code you're running, you should use a more restrictive security manager.

提交回复
热议问题