From Java documentation:
Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
Also, Local class is a special kind of inner class, which needs to be instantiated from an outer class object:
As with instance methods and variables, an inner class is associated with an instance of its enclosing class
But static block is invoked during class load, before an object instance creation.
So, how is it possible create and instantiate a local class inside a static initializer block?
For instance:
class OuterClass {
static{
class InitLocalClass {
...
}
InitLocalClass lc = new InitLocalClass();
}
}
Answer adapted from dup for Local Classes: as per JLS item # 15.9.2:
If C is a local class (§14.3), then let O be the innermost lexically enclosing class of C. Let n be an integer such that O is the n'th lexically enclosing class of the class in which the class instance creation expression appears. Then:
If C occurs in a static context, then i has no immediately enclosing instance.
Otherwise, if the class instance creation expression occurs in a static context, then a compile-time error occurs.
Otherwise, the immediately enclosing instance of i is the n'th lexically enclosing instance of this (§8.1.3).