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(){

    public class BBar {
        public void anotherMethod(){
// then called using: //
Foo myFoo = new Foo();


  • Keeping the last visible item of a ListView when the size of the ListView changes
  • Import .csv file to Sqlite in Android
  • Getting android.content.res.Resources$NotFoundException: exception even when the resource is present in android
  • Android aapt high processor utilization (and slow!)
  • AppcompatActivity Error with Android studio
  • Promo Video not visible on Google Play, but works on Android Play
  • 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 {
            // ...
            public void onDrawFrame(GL10 gl) {
        public class Stage extends MovieClip {
            public float alpha = 0f;

    Related posts:

    Android what does the clipToPadding Attribute do?
    How to get the date from the DatePicker widget in Android?
    why is requestIdToken returning null?
    Creating an Android Lock Screen App.
    Android WebView -> Display WebArchive
    Xamarin forces me to download Android SDK and NDK
  • What is the standard way to organize Android code in project
  • Android SSL - SNI support
  • SSL handshake aborted Connection reset by peer while calling webservice Android
  • FragmentTabHost graphical layout doesn't render
  • Android: Proper Way to use onBackPressed() with Toast
  • Successful Purchase but has Class not found when unmarshalling:
  • 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

    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.