Java – Build error when using static inner classes

Build error when using static inner classes… here is a solution to the problem.

Build error when using static inner classes

I’m working on a library for Android, and when I use it in my sample project, I keep getting strange errors about static inner classes: “Symbolic variable not found”:

Here is my decomposition code :

Outer.java (SDK project).

public class Outer {

[...] // Attributes and methods

public static class Inner {
        public static int x = 42; 
    }
}

MainActivity .java (sample project).

import com.xxx.xxx.Outer;

public class MainActivity extends Activity {

[...] // Attributes

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Log.d("MainActivity", "x = " + Outer.Inner.x);
    }
}

This is a log error:

…/sample/MainActivity.java:54: error: cannot find symbol
Log.d(“MainActivity”, “x = ” + Outer.Inner.x);
^

symbol: variable Inner
location: class Outer

I have another case in my SDK:

My class “

ImageHelper” has a static inner class “Builder”.

I use it in

my SDK and it works, but when I want to use it in the sample project, I get the same error when I build run.

Is there an error in my code?

Solution

Did you try to compile your own cut code? It works fine when I compile it myself. Maybe it (providing clips) also works for you and your complete program uses a different class?

Based on your code

import com.xxx.xxx.Outer;

And the package line is missing in the Outer code, your project may not have one Outer, but many, one of which has no internal static class.

Related Problems and Solutions