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

后端 未结 7 1927
暗喜
暗喜 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:28

    Reflection is dangerous. Period.

    What's the point in limiting the utility of a really dangerous system for the sake of ever so slightly increased safety?

    Also, automated serialization requires the ability to "suck the brains" out of any class; ignoring access modifiers is a necessity in this case.

提交回复
热议问题