Java: Class.this explained

SomeClass.this is used inside anonymous classes to refer to the enclosing class.

Here's an example:

class Enclosing {
    void method(Enclosing other) {
        // ...
    }

    void otherMethod() {
        new Runnable() {
            public void run() {

                method(this);Does not compile. 'this' is a Runnable!




                method(Enclosing.this);Compiles fine.



            }
        }.run();
    }
}

Comments

Be the first to comment!