I recently ran into something like this...
public final class Foo<T>
implements Iterable<T> {
//...
public void remove(T t) { /* banana banana banana */ }
//...
public Iterator<T> Iterator {
return new Iterator<T>() {
//...
@Override
public void remove(T t) {
// here, 'this' references our anonymous class...
// 'remove' references this method...
// so how can we access Foo's remove method?
}
//...
};
}
}
Is there any way to do what I'm trying to while keeping this as an anonymous class? Or do we have to use an inner class or something else?
To access remove
in the enclosing class, you can use
...
@Override
public void remove(T t) {
Foo.this.remove(t);
}
...
Related question: Getting hold of the outer class object from the inner class object
Foo.this.remove(t) will do the trick for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With