Using an expression such as A.this
requires that A
is the directly enclosing class. In this case, it may look like A
is the directly enclosing class, but in fact it's not. The directly enclosing class is the anonymous subclass of A
. (I.e. A.this
is an attempt to skip over the anonymous class in the hierarchy.)
In this case an expression such ____.this
won't work, because your class is anonymous, which means that there's no name to put in front of .this
.
The way you've solved it is reasonable in my opinion. Another way would be to create a local subclass of A
as follows:
public static void main(String[] args) { class SubA extends A { @Override public void m() { System.out.println("Override - > m()"); new Thread(new Runnable() { @Override public void run() { SubA.super.m(); // ^^^^ we now have a name of the directly enclosing class } }).start(); } } A a = new SubA(); a.m(); }
Read full article from java - Calling super method from within an anonymous inner class inside the overriden method - Stack Overflow
No comments:
Post a Comment