Why can't I have a two-level-deep inner class with the same name as its containing class?
Java lets you refer to an outer class without fully specifying its name, like this:
public static class Inner1
{
public static class Inner2
{
public static class Inner3
{
public void demo() {
Class<Inner2> c = Inner2.class; // This is allowed
}
}
}
}
Had nesting of classes allowed the use of identical names at any level of hierarchy, referencing by unqualified name would have been impossible. It is this ability that the Java compiler is trying to preserve by prohibiting nested declarations to collide with names of their outer classes.