In Java, generally speaking, this pointer refers to the object of the code currently being accessed. However, if you need to use an object in an external class in an internal class, you need to use the class name of the external class to qualify it. This method is also relatively common in Android development.
@Author: twlkyaopackage twlkyao;public class A { public A() { Inner inner = new Inner(); inner.outer(); // call the inner class's outer method. this.outer(); // call A's outer method . } public void outer() { System.out.println("outer run"); } class Inner { public void outer(){ System.out.println("inner run"); A.this.outer(); // call A's outer method. System.out.println("--------"); } } public static void main(String[] args) { A a = new A(); }}
Inner is an inner class that accesses the outer() method in class A. Since the anonymous inner class has the same method, it needs to be qualified using A's this pointer.
The output is:
inner runouter run--------outer run