Accessing occluded outer class members and methods from inner class

I’m posting so it’ll help me remember – I aways forget that stuff, because I rarely need it.

Okay, so, you got class “A”, which is an “outer” class, which defines “B” as an “inner” class (called A.B).

class A {

 public void someMethod() {}
public class B {
}

}

Let’s say an instance of A.B wants to call a method from A.

That’s easy, because methods and members from A are directly visible to B.

The issue comes when B wants to class a member or field of A that has the same name as a member or field of B.

The this “pointer” becomes kind of ambiguous. When in “B”, this refers to the instance of “B”, and not to the instance of “A”.

I’m here to tell you that yes, you can access the this of A!

Ready? Here’s the syntax – that will only work while within “B” (of course).

Lets say that you wanted to call the “toString()” method of “A” from “B”:

A.this.toString()

Yes, that is “<outerclassname>.this.<member or field name>”.

Have a happy day.