i have a class
main {
Class1 class1=new Class1();
class1.function1();
}
class Class1 {
int abc=1;
ArrayList<Class2> class2s=new ArrayList<Class2>();
int function1() {
class2s.add(new Class2(asd));
abc=555;
}
}
class Class2 {
int functionx() {
Log.e("abc?", ""+PARENT???.abc);
}
}
How can I get the caller Class’s variable, say abc?
You could pass the caller as an argument, like so:
and call it with
As long as abc is visible to Class2. Otherwise there is no direct means of knowing your caller in Java. The variable class2s implements a uni-directional relationship between Class1 and Class2. So you can only navigate from Class1 to Class2, not the other way around.