In Java, can these / super keywords represent anything other than classes / enumerations?

I noticed that:

class A {
    ClassB b = new ClassB() { // anonymous class
        /* some expression using this */
    }
}

Whenever I use a keyword thisinside an anonymous class, it thisrefers to the surrounding outer class / enumeration, and not to the anonymous class.

Does this mean that it thiscan never represent an anonymous class? Just "normal" classes and enumerations?

Also, can thisor superrepresent an interface?

+3
source share
5 answers

this . this , . , OuterClassName.this.

this super , .

+5

- this , , .

+6

A.this .

, anynomous , .

EDIT: .

0

Java this , . , this super .

0

. , java.lang.Object, java.lang.Object super.

Runnable r = new Runnable() {
    public void run() {
        super.run(); // Error: run() is not a method of java.lang.Object
        super.toString(); // OK: toString() is inherited from java.lang.Object
    }
};
0

Source: https://habr.com/ru/post/1787000/


All Articles