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

后端 未结 7 1926
暗喜
暗喜 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:53

    Reflection is a complete API for getting inside classes. Private members and all.

    In cases where you don't trust the code you're running (applets and the like) you can prevent the code from using reflection at all. See this Stack Overflow question for details.

    0 讨论(0)
提交回复
热议问题