You cannot access the instance of the caller unless the instance is somehow transferred to it or stored in the collection.
To transfer an instance, you can do the following:
class A{ void foo(Object caller){ long val1 = .. // do something where val1 is crucial } } class B{ double val1 = Math.random(); public static void main(String[] args) { new B().callFoo(); } void callFoo(){ new A().foo(this); } }
The key "this" will pass the instance of the calling code to the foo method in class A
source share