implementation inconsistency with scope rules for Java generic types

Alex Buckley Alex.Buckley at Sun.COM
Fri Jun 20 14:09:42 PDT 2008

Yes, JLS 8.1.6 defines the class body as the content strictly within the 
{ ... } of the class declaration.


Rémi Forax wrote:
> Maurizio Cimadamore a écrit :
>> Hi Nick,
>> this is definitively an interesting question; if your example might 
>> look like a straight javac bug at first, it' less clear whether it 
>> should be considered as such when Subclass and NestedSubclass live in 
>> completely unrelated packages; I think that one in principle could 
>> exploit an eventual vulnerability (as the one you described for 
>> Eclipse) for acquiring access to members of a private type that 
>> otherwise cannot be accessed.
>> In this particular case however, since the class referencing the 
>> private type is the class in which that type is defined, I don't think 
>> that an error should be raised - so this is indeed a javac bug.
>> Alex do you have any JLS paragraph handy to support this?
> In section 6.6.1, the  next to last bullet  says:
> "if the member or constructor is declared |private|, then access is 
> permitted if and only if it occurs within the body of the top level 
> class (§7.6) 
> <> 
> that encloses the declaration of the member or constructor"
> If body means body like in the grammar;
> /NormalClassDeclaration:
> /|     class|/ Identifier [/TypeParameters]/ [/|extends|/ Type] 
> [/|implements|/ TypeList] ClassBody/
> Annotations or extends/implements types are not in the body so javac is 
> correct.
>> Maurizio
> Rémi
>> Nick Radov wrote:
>>> I've run across an interesting inconsistency between javac and the 
>>> Java compiler built in to the Eclipse 3.3.2 IDE. Try compiling this 
>>> piece of code.
>>> public class X<T> {
>>> }
>>> class Subclass extends X<Subclass.NestedSubclass> {
>>> @SuppressWarnings("unchecked")
>>> private final class NestedSubclass extends X {
>>> }
>>> }
>>> The javac compiler reports one error because the generic type 
>>> parameter of the Subclass class has private access, but Eclipse 
>>> reports no errors. I had reported this as an Eclipse bug, but one of 
>>> the Eclipse team thinks it may be a javac bug. You can see the 
>>> Eclipse bug report here: 
>>> <>.
>>> Does anyone know for sure which is correct? As far as I can tell the 
>>> JLS doesn't explicitly state whether this is allowed, although it may 
>>> be implicit in the scope rules.
>>> *Nick Radov | Director, Research & Development | Axolotl Corp*
>>> <>, d: 408.920.0800 x116, f: 
>>> 408.920.0880
>>> 160 West Santa Clara St., Suite 1000, San Jose, CA, 95113
>>> /The information contained in this e-mail transmission may contain 
>>> confidential information. It is intended for the use of the 
>>> addressee. If you are not the intended recipient, any disclosure, 
>>> copying, or distribution of this information is strictly prohibited. 
>>> If you receive this message in error, please inform the sender 
>>> immediately and remove any record of this message./ 

More information about the compiler-dev mailing list