Compiler Error J0195

Cannot declare 'identifier' as 'static' in inner class 'identifier'

The compiler detected an attempt to declare a variable or method as static from within an inner class. Unlike regular class declarations, inner classes do not support static members. Classes defined within another class, that are declared as static, can have static members but the class is considered an outer class. This error can also occur if an interface was defined within an inner class.

The following example illustrates this error:

public class Simple{
   
   //Do something meaningful here
   
   class InnerClass{
   
   static int var1; /* error: cannot declare static
                              members in inner class*/
   }
}

The following example illustrates how to define a class within a class that can contain static members:

public class Simple{
   
   //Do something meaningful here
   
   /*Because 'InnerClass' class is declared as static it is now treated
     as an outer class enclosed within the 'Simple' class*/
   static class InnerClass{
      static int var1 = 100; // This is OK as long as the class is static
   }
}