views:

746

answers:

3

In the following snippet

public class a{

public void otherMethod(){}
public void doStuff(String str, InnerClass b){}
pubic void method(a){

  doStuff("asd",
      new InnerClass(){
         public void innerMethod(){
              otherMethod();
         }
      }
  );
}

}

Ss there a keyword to refer to the outer class from the inner class? Basically what I want to do is outer.otherMethod(), or something of the like, but can't seem to find anything?

+10  A: 

In general you use OuterClassName.this to refer to the enclosing instance of the outer class.

In your example that would be a.this.otherMethod()

Bill the Lizard
A: 

@Bill the lizard I think that only works to reference static elements.

reread, didn't see the .this at first. thats works, thanks.

shsteimer
the .this implies that you have an instance of the class.
jjnguy
The "this" bit is for instance members. For static members it would be just the class name.
Dan Dyer
+2  A: 
OuterClassName.this.outerClassMethod();
jjnguy