java - Access the instance from current instance -


i want instance of class, instance has been made, possible?

note: classes not nested.

    class frame {         protected panel p1;         private string name;         public frame(string name) {             this.name=name;             p1=new panel();         }         public string getname() {             return name;         }     } 

..

    class panel {         public panel() {             system.out.println("i made in: "+frame.this.getname());                                      // can't         }        } 

..

    public void main(string[] args) {         frame f1=new frame("first"); // should print: first         frame f2=new frame("second"); // should print: second     } 

note 2: aware of approach:

 class frame {         protected panel p1;         private string name;         public frame(string name) {             this.name=name;             p1=new panel();         }         public string getname() {             return name;         }     } 

..

    class panel {                 private frame owner;         public panel(frame owner) {                         this.owner=owner; // declaring owner             system.out.println("i made in: "+owner.getname());           }        } 

..

    public void main(string[] args) {         frame f1=new frame("first"); // should print: first         frame f2=new frame("second"); // should print: second     } 

...but doesn't make me happy, want somehow directly access, if possible.

declare instance variable of type b in class a. accept parameter of type b in constructor a.

in b's constructor, pass this constructor a.

class {     b b;     a(b b) {         this.b = b;         // instance of b can call this, example:         b.geta();     } }  class b {     a;     b() {        a=new a(this);     }     geta() { return a; }  } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -