I.e. why is the following "cyclic dependency" not possible?
public class Something implements Behavior {
public interface Behavior {
// ...
}
}
Since interfaces don't reference the outer class this should be allowed; however, the compiler is forcing me to define those interfaces outside the class. Is there any logical explanation for this behavior?
Relevant rules in spec:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.4
http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.1.3
Therefore if
A extends|implements B.C, A depends on bothCandB. Spec then forbids circular dependencies.The motivation of including
Bin the dependency is unclear. As you mentioned, ifB.Cis promoted to top levelC2, not much is different as far as the type system is concerned, so whyA extends C2is ok, but notA extends B.C? Granted a nested typeB.Cdoes have some prviledged access toB's content, but I can't find anything in spec that makesA extends B.Ctroublesome.The only problem is when
Cis an inner class. SupposeB=A,A extends A.Cshould be forbidden, because there's a circular dependency of "enclosing instance". That is probably the real motivation - to forbid outer class from inheriting inner class. The actual rules are more generalized, because they are simpler, and make good sense anyway even for non-inner classes.