Commentary:
They can if the static variable is also made final.
Is this only for primitive member variables? Attempting to declare a final reference variable in a non-static inner class results in a compiler error, in this case at least:
import java.util.Date;
class Test{
class Inner{
final static Date date = new Date(); //illegal static declaration
}
public static void main(String[] args){}
}
It is a compile-time error if an inner class declares a member that is explicitly or
implicitly static , unless the member is a constant variable (§4.12.4).
And as per 4.12.4,
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28)
So for example, static final String s = ""; compiles but static final String s = new String(); doesn't.
Since Java 16, Inner classes can declare non-final static members as well.
To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class.
It is helpful at this point to abuse the terminology somewhat, and say, loosely, that the static keyword always marks a "top-level" construct (variable, method or class), which is never subject to an enclosing instance.
To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class.