Access outer ‘parent’ class from inner class in Java

Sometimes in Java you’ll create an inner or anonymous class, and need to access the containing class. The code is fairly simple:

ParentClass.this

ParentClass is the class type, this will resolve to the instance that contains the instance of the inner class. You can access methods using this too:

ParentClass.this.myMethod();

Hope this helps!

Leave a Reply