網頁

2010年5月20日 星期四

Java Nested Classes: this

http://mindprod.com/jgloss/this.html

"this" means the current object, the instance we are currently running a method on.

Beware, inside an (anonymous) inner class, "this" refers to the inner class, not the usual enclosing one. If the outer class were called MyOuter, you could get at its this with "MyOuter.this".

Example:

public class MyClass {
  private String outerClassField = "some value";
 
  public void outerClassMethod() {
    someObject.addMyListener(new MyListener() {
      public void processEvent(MyEvent e) {
        MyClass.this.outherClassField;
      }
    });
  }
}

沒有留言: