Java – why can’t a class extend a static nested class?

This class:

public class OuterChild extends OuterChild.InnerParent {
    public static class InnerParent {
    }
}

Unable to compile:

$javac OuterChild.java
OuterChild.java:1: error: cyclic inheritance involving OuterChild
public class OuterChild extends OuterChild.InnerParent {
       ^
1 error

Because outerchild will "depend" on itself, because (each § 8.1.4 "superclasses and subclasses" of the Java language specification, Java se 8 edition) a class directly depends on "any type mentioned in its extension or implementation clause [...] as a fully qualified form of superclass or super interface name"

But I don't quite understand the motivation here What are problematic dependencies? Is this just to be consistent with the case where innerparent is not static (so it will eventually contain its own lexical closed instances)?

Solution

This seems to be a pretty bad role, because there is a number of bugs related to loop inheritance, which usually leads to infinite loops, stack overflow and oom in the compiler Here are some relevant quotes that may provide some insight:

Bug 4326631:

Bug 6695838:

Bug 8041994:

Bug 6660289:

Bug 6663588:

For your specific question "what is problematic dependency"? It seems to be a complex compile time symbol parsing edge case. The solution introduced in jls2 is to simply prohibit the loop introduced by qualifier types and actual superclasses

In other words, in theory, this can lead to appropriate improvements in the compiler, but this unusual relationship can't be prohibited in the language specification until someone happens and happens

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>