Java nested inner classes access external class variables
•
Java
Can nested inner classes abar and bbar access variables of the main class? For example:
public class Foo { public ABar abar = new ABar(); public BBar bbar = new BBar(); public int someCounter = 0; public class ABar { public int i = 0; public void someMethod(){ i++; someCounter++; } } public class BBar { public void anotherMethod(){ bbar.someMethod(); someCounter++; } } } // then called using: // Foo myFoo = new Foo(); myFoo.bbar.anotherMethod();
edit
It seems that the code I typed will work if I try it first; Try to get help, not too specific I'm actually in trouble
Failed due to the error "cannot make static reference to non static field",
public class Engine { public Stage stage = new Stage(); // ... public class Renderer implements GLSurfaceView.Renderer { // ... @Override public void onDrawFrame(GL10 gl) { stage.alpha++; } } public class Stage extends MovieClip { public float alpha = 0f; }
Solution
In your code, yes, this is possible
See: nested classes
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码