Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an outer class from inside a listener?

I have a listener inside Class A, and I want to pass Class A to my Class B inside the listener. Normally I'd just use this, but then I'd get the event that triggered the listener.

like image 345
they changed my name Avatar asked Nov 26 '09 23:11

they changed my name


People also ask

How do you access the outer class variable in an inner class?

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.

Can inner class access private members of outer class in Java?

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

How do you reference an outer class in Java?

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

Can static inner class access outer class variables?

Unlike inner class, a static nested class cannot access the member variables of the outer class. It is because the static nested class doesn't require you to create an instance of the outer class.


1 Answers

A.this.

(It is rare that the inner class this is useful. Indeed it is relatively common to have bugs where the wrong this was used. So it is unfortunate that it is the default. Not about to change after 12 years.)

like image 71
Tom Hawtin - tackline Avatar answered Oct 20 '22 08:10

Tom Hawtin - tackline