4

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!

9ee1
  • 1,078
  • 1
  • 10
  • 25
  • @KublaiKhan Yeah, I realize my code sample was not a good indicator of what I wanted to do. I posted some updated code. – 9ee1 Dec 12 '11 at 22:05
  • I'm interested, but could you please narrow this down to an [SSCCE](http://sscce.org/) so it's easier for us to help you? Eliminate unrelated code and just show us the issue. – Paul Bellora Dec 12 '11 at 22:10
  • Sorry about that. Made the code more to the point. – 9ee1 Dec 12 '11 at 22:18
  • Is this a duplicate of http://stackoverflow.com/questions/7103087/why-cant-i-use-class-this-in-anonymous-class ? – waxwing Dec 12 '11 at 22:49

2 Answers2

1

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();
            }
        };
    }
GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • Seems that this does it. Was kind of hoping there was another way. Thanks for your answer. – 9ee1 Dec 13 '11 at 20:32
0

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();
      }
   }
}
GETah
  • 20,922
  • 7
  • 61
  • 103