Let's look at this example:
public class MainClass { public class NonStaticClass { public static NonStaticClass nonStatic = new NonStaticClass();
The problem is that NonStaticClass , well, is not static . A non-stationary inner class cannot contain static fields.
If you want to have a static field in the inner class, you need to make the class static.
From the java documentation:
Inner classes
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to this methods and object fields. Also , because the inner class is associated with an instance, it cannot define any static members of itself .
For more information, see Nested Classes
source share