Java nested inner class access outer class variables

Is it possible for the nested inner classes ABar and BBar to access main class’s variables? 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

  • Android WebView focus problem
  • Android - How to Dismiss All Dialogs in onPause
  • Why RecyclerView.OnScrollListener is not an interface but an abstract class?
  • How do I enforce a circular reference in a RelativeLayout?
  • Checking Host Reachability/Availability in Android
  • Android setUserVisibleHint never gets called?
  • Seems the code I typed would have worked if i’d have tried it first; was trying to get help without being too specific. The code I’m actually having trouble with

    Fails because of the error ‘cannot make static reference to the non-static field stage’

    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;
        }
    

    Related posts:

    Defining a percentage width for a LinearLayout?
    NineOldAndroids, not clickable view after rotation or move
    How to get the keys from ContentValues?
    Push Listview when keyboard appears without adjustPan
    How to change the hint size of TextInputLayout
    Photoshop design from px to dp
  • OnClick event only works second time on edittext
  • Android: how to put question mark in a string resource?
  • Invalidate cache in Picasso
  • android, how to get directory listing?
  • How to use hardware accelerated video decoding on Android?
  • Recording video using MediaRecorder and FileOutputStream produces video file that can't be played
  • 2 Solutions collect form web for “Java nested inner class access outer class variables”

    In your code, yes, it is possible.

    Non-static nested classes (inner classes) have access to other members
    of the enclosing class, even if they are declared private. Static
    nested classes do not have access to other members of the enclosing
    class.

    See: Nested Classes

    If your inner class extends the outer class, then it will have access to the outer class public and protected members. I just tired it and it worked. The construct is a bit odd, because it implies a sort of infinite loop in the class definition, but it seems to do the job.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.