I recently use this code, and realize that in anonymous class, I can’t access the instance by .this, like this:
Sprite sprFace = new Sprite() {
@Override
protected void onManagedUpdate(float pSecondElapsed) {
runOnUpdateThread(new Runnable() {
@Override
protected void run() {
Sprite.this.getParent().detach(Sprite.this); // Here
}});
}
};
I know how to solve it (just declare a “me” variable), but I need to know why I can’t use <Class>.this?
The
<Class>.thissyntax gives a special way of referring to the object of type<Class>, as opposed to the shadowing type.In addition,
<Class>must be the name of the type you’re trying to access. In your case,Spriteis not the actual type ofsprFace. Rather,sprFaceis an instance of an anonymous subclass ofSprite, thus the syntax is not applicable.