Search This Blog

Access private field via reflection in Java

public class A {

    private int i = 5;

}

public class B {

    public static void main(String[] args) {
        A a = new A();
        Field iField = a.getClass().getDeclaredField("i");
        iField.setAccessible(true);
        return iField.getInt(a);
    }

}



No comments:

Post a Comment