java - Can an attribute object reach the object for which it is an attribute? -


i'm sorry question might odd, time ago used python's introspection, , wondering if might available in java 1.8.

so let's have object a class a in situation this

class a{ b b; } class b{ public void f(){} } 

my question "can f() know a?"

clearly, can, if add backward link, like:

class {     b b;       public a() { b = new b(this); }  }  class b {     father;      public b(a a){ this.father = a; }      public void f(){}  } 

but question is, in general setting, without father or so, there introspection mechanic allow f() know a?

you wish access arguments of function upper in call stack. in vanilla java can't accomplish that, can use aop framework, maintain shadow stack, can introspect it.

or might use inner classes. inner classes contain hidden field instance of outer class. can accessed syntax outerclass.this.

class a{   public class b{      public void f() {       system.out.println(a.this.afield);     }       }   string afield;   b b = new b(); // passes hidden `this` parameter } 

Comments