Error in eclipse or javac compiler?

Who is right? Eclipse or javac?

--------------- c / v / A.java ---------------

package cv; public class A<T> { } 

--------------- c / v / B.java ---------------

 package cv; public class B extends A<B.Secret> { private class Secret {}; } 

Eclipse compiles B.java just fine.

Javak has a problem.

  $ javac c / v / B.java
 c / v / B.java: 3: cvBSecret has private access in cvB
 public class B extends A <B.Secret> {
                            ^
     1 error
+7
compiler-construction generics eclipse javac
source share
3 answers

Relevant sections of the Java Language Specification should be:

ยง8.1.4: [...] The ClassType type must indicate the available class type (ยง6.6), or a compile-time error occurs.

ยง6.6.1: [...] A member (class, interface, field or method) of a reference type (class, interface or array) or a class type constructor is available only if the type is available and the member or constructor is declared to allow access :

  • If a member or constructor is declared public, access is allowed. All interface members are implicitly public. [...]
    • Otherwise, if a member or constructor is declared private, access is allowed if and only if it occurs inside the body of a top-level class (ยง7.6), which includes the declaration of a member or constructor.

Since ClassType is not part of the class body, B.Secret not available at this location, therefore A<B.Secret> not available, therefore a compile-time error should occur.

+7
source share

Eclipse is wrong. If you advertise something like

 extends A<X> 

you need to know both A and X.

+6
source share

I would like to think that Javak is right. To create a new class

 A<B.Secret> 

the general must have access to the class that it uses. The fact that B then extends this class is negligible.

+3
source share

All Articles