Are private members really more “secure” in Java?

后端 未结 8 2912
后悔当初
后悔当初 2021-02-19 03:23

Learning Java I was sometimes taught to use the private access modifier so as not to expose \"sensitive information\" to other classes, as if this could open a legi

8条回答
  •  萌比男神i
    2021-02-19 03:50

    I agree in general with the answers so far (i.e. that private is really for code hygiene not real security). Various answers have pointed out that you can bypass private using reflection. Note that you can, in turn, disable reflection if you enable a Java SecurityManager. See particularly the ReflectPermission. However, security managers are rarely used (outside the normal browser sandboxing).

提交回复
热议问题