Accessing the containing class of an inner class in Java
Asked Answered
W

3

19

This is what I'm doing now. Is there a better way to access the super class?

public class SearchWidget {
    private void addWishlistButton() {
        final SearchWidget thisWidget = this;
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                // A better way to access the super class?
                // something like "this.super" ...?
                workWithWidget(thisWidget);
            }
        }
    }
}

I'm programming with Google Web Toolkit, but I think this is really a generic Java question.

Winola answered 18/5, 2010 at 15:19 Comment(3)
"super" refers to the class one level up the inheritance hierarchy, which is misleading since inheritance has nothing to do with what you want. You want access to the containing class.Humidity
Agree! Question is phrased incorrectly and I am wondering why/who up voted this. Martijn Courteaux has given the answer you are looking forWrapped
Out of curiosity, why do you need to interact with the super class? All static methods declared in the super class also appear in its children, as well as any non-overridden public/protected methods... and one must assume methods are overridden for a reason.Octameter
M
26

You can use what is called the qualified this.

JLS 15.8.4. Qualified This

Any lexically enclosing instance can be referred to by explicitly qualifying the keyword this.

Let C be the class denoted by ClassName. Let n be an integer such that C is the n-th lexically enclosing class of the class in which the qualified this expression appears. The value of an expression of the form ClassName.this is the n-th lexically enclosing instance of this (§8.1.3). The type of the expression is C. It is a compile-time error if the current class is not an inner class of class C or C itself.

In this case, you can do what Martijn suggests, and use:

workWithWidget(SearchWidget.this);

References

Related questions

Mazzard answered 18/5, 2010 at 17:0 Comment(1)
The JLS links don't go to the spec itself now, but rather a containing page. Suggest updating the links.Adjutant
E
20

You can write the name of the outer class and then .this. So:

workWithWidget(SearchWidget.this);
Exemplificative answered 18/5, 2010 at 15:21 Comment(1)
@Gerdemb: Start accepting answers on your questions. You have only 24% accepted answers. If you never accept answers, people are going to left your question for what it is, because you will not accept them.Exemplificative
T
2

To access super of the object that contains an object of an anonymous class from that object, try, in your case SearchWidget.super


Example:(see the third call Child.super.print())

public class Test1 {
public static void main(String[] args) {
    new Child().doOperation();
}
}

class Parent {
protected void print() {
    System.out.println("parent");
}
}

class Child extends Parent {
@Override
protected void print() {
    super.print();
    System.out.println("child");
}

void doOperation() {
    new Runnable() {
        public void run() {
            print();              // prints parent child
            Child.this.print();   // prints parent child
            Child.super.print();  // prints parent
        }
    }.run();

}
}
Trousers answered 18/5, 2010 at 15:22 Comment(5)
How did you come up with this syntax?Wrapped
in my case, per accident. I used methods from the container class and eclipse added the qualifier.Tortuous
@ring bearer: I added an example.Trousers
Alright, can you use this as a variable? for example can you call a testMethod(Parent p) as testMethod(Child.super) - I guess NO?Wrapped
Yes, it does not make sense to call testMethod(Child.super), since there is only one object, calling super does not give you another object. However, the OP didn't specify in the title correctly what he wanted (the original title was "Accessing the super of this...") and first I understood something else.Trousers

© 2022 - 2024 — McMap. All rights reserved.