Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Outer Class this instance

Tags:

how do we access outer class this instance: eg in

Class A {

   Class B {

      this.helloB();
      (A's this).hello()
   }
}

how do we access A's this instance in Java

like image 594
sachin Avatar asked Nov 12 '09 11:11

sachin


People also ask

How do you reference an outer class?

In general you use OuterClassName. this to refer to the enclosing instance of the outer class.

How do you access the outer class variable in Python?

The variables that are defined outside the class can be accessed by any class or any methods in the class by just writing the variable name.

How does an inner class instance access the outer class members?

Since inner classes are members of the outer class, you can apply any access modifiers like private , protected to your inner class which is not possible in normal classes. Since the nested class is a member of its enclosing outer class, you can use the dot ( . ) notation to access the nested class and its members.

How to access outer class variable in inner class in c#?

If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.


2 Answers

Just call

A.this.hello()
like image 86
alasdairg Avatar answered Sep 18 '22 04:09

alasdairg


By prefixing this with the class: A.this.hello()

Similarly, when you want to create an instance of B outside of A, you can use a.new B() (where a instanceof A == true).

like image 25
Aaron Digulla Avatar answered Sep 19 '22 04:09

Aaron Digulla