For example, there is a code:
class B { private int i = 22; } class A { // тут код который выводит переменную i }
Condition: Class B cannot be changed. class A can be changed (ie, inherit class B, etc.)
For example, there is a code:
class B { private int i = 22; } class A { // тут код который выводит переменную i }
Condition: Class B cannot be changed. class A can be changed (ie, inherit class B, etc.)
Through Reflection
B b = new B(); Class clazz = B.class; Field iField = clazz.getDeclaredField("i"); iField.setAccessible(true); int fieldValue = iField.getInt(b); System.out.println("i = " + fieldValue);
I think it's easier to add a method to class A.
public int getI(){ return i; }
the so-called getter
.
Source: https://ru.stackoverflow.com/questions/101909/
All Articles