Why is it allowed to access Java private fields via reflection?

后端 未结 7 1930
暗喜
暗喜 2020-12-08 04:14

Consider this example :

import java.lang.reflect.Field;

public class Test {

    public static void main(String[] args) {
        C c = new C();
        try         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 04:32

    Private is intended to prevent accidental misuse, not as a security mechanism. If you choose to bypass it then you can do so at your own risk and the assumption you know what you are doing.

提交回复
热议问题