Access quotthisquot from Java anonymous class

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Given the following code:

public interface Selectable {
  public void select();
}

public class Container implements Selectable {
  public void select() {
  ...
  }
  public void createAnonymousClass() {
    Selectable s = new Selectable() {
      public void select() {
        //see comment below.
      }
    };
  }
}

I want to access Container.select() from within my anonymous class select() method. However, this.select() would again call the anonymous class select() method.

My suggestion would be:

Introduce a field into Container, e.g.

private Container self = this;

Now I can access Container.select() by calling self.select() from within the anonymous class.

Is this a reasonable way? Or are there any better ways?

Answers

Container.this.select();

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1084112/access-this-from-java-anonymous-class

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils