Reputation: 1078
Okay, so I know how to access the outer class that encloses an inner class, whether its anonymous or inner.
But my question is, how to access the outer class if it itself is an inner class? Some code to help:
public final class LocationPage extends BasePage {
private static final String CRITERIA_FORM_ID = "CriteriaForm";
protected Panel onCreateBodyPanel() {
return new ViewBodyPanel(BasePage.BODY_PANEL_ID) {
public void invokeMe() {
// How do I Invoke This Method?
}
private Form<CriteriaBean> helpCreateCriteriaForm() {
return new Form<CriteriaBean>(LocationPage.CRITERIA_FORM_ID) {
@Override
protected void onSubmit() {
LocationPage.this.ViewBodyPanel.invokeMe(); // Compile Error.
}
};
}
};
}
}
UPDATE: For those wanting to see what I am trying to do here, here is a complete code sample. This is actually Apache Wicket specific but I think you can get the idea. Have a look at a method named onSubmit. I added a code comment to help pinpoint it.
UPDATE TWO: Made the code sample to the point. Sorry about that!
Upvotes: 4
Views: 162
Reputation: 17846
You only need to specify ParentClass.this.something to disambiguate. If your Form does not have a method invokeMe, you can simply use the name without qualifying, and the compiler should find it:
private Form<CriteriaBean> helpCreateCriteriaForm() {
return new Form<CriteriaBean>(LocationPage.CRITERIA_FORM_ID) {
@Override
protected void onSubmit() {
invokeMe();
}
};
}
If the function does exist in the inner-inner class as well, there's no trick in Java to do it. Rather rename or wrap your ViewBodyPanel.invokeMe method into something that is unambiguous.
public void vbpInvokeMe(){
invokeMe();
}
private Form<CriteriaBean> helpCreateCriteriaForm() {
return new Form<CriteriaBean>(LocationPage.CRITERIA_FORM_ID) {
@Override
protected void onSubmit() {
vbpInvokeMe();
}
};
}
Upvotes: 1
Reputation: 21419
Okay, so I know how to access the outer class that encloses an inner class, whether its anonymous or inner.
If you are using inner classes than you can do the following
public class A{
public void printHello(){
System.out.println("Hello!");
}
class B{
public void accessA(){
A.this.printHello();
}
}
}
Upvotes: 0