This class
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.new A();
m.new B(); //1 - compilation error
new Main.B();
}
class A{}
static class B{} //2
}
will result in a compile time error at line 1:
Illegal enclosing instance specification for type Main.B
But I cannot understand why, I find it a bit counterintuitive: at line 2 we have a static class definition, shouldn'it be accessible from object m as well?
If Main
had a static variable i
, m.i
wouldn't result in a compilation error. Why is the behaviour different with class definition?
No.
The whole point of a static inner class is that it doesn't have an instance of the enclosing class.