Re: [Java Spec Report] reference to nonstatic type from static context

Neal M Gafter <[email protected]> Wed, 07 Apr 2004 18:05:33 -0700
Newsgroups gmane.comp.java.spec-report
Organization Sun Microsystems
Message-ID <[email protected]>
Eric Blake wrote:
> If I understand you correctly, then what compilers should enforce is the 
> following:
> 
> package p;
> class A<T> {
>    class B {
>      T t;
>    }
>    static class C {
>      B b1; // illegal, accessing non-static member B by simple name
>      A.B b2; // legal, accesses raw type, so T becomes erasure of Object
>      A<String>.B b3; // legal, accesses parameterized type, T=String
>      {
>         b1.t = ""; // illegal, since b1 was illegal
>         b2.t = ""; // legal, t treated as Object
>         b3.t = ""; // legal, t treated as String
>      }
>    }
> }
> 
> package p;
> class D extends A.B { // legal, extends raw type, with T as Object
>    D() {
>      new A().super(); // necessary to extend an inner class
>      t = ""; // legal, treated as Object
>    }
> }
> 
> package p;
> import p.A.B;
> class E extends B { // legal, extends raw type, with T as Object
>    E() {
>      new A().super(); // necessary to extend an inner class
>      t = ""; // legal, treated as Object
>    }
> }
> 
> package p;
> class F extends A<String>.B { // legal, extends parameterized type, T=String
>    F() {
>      new A<String>.super(); // necessary to extend an inner class
>      t = ""; // legal, treated as String
>    }
> }

All true.

> This means the requirement in 8.5.2 is still poorly worded - "usage of a 
> non-static member" is not defined, that I could see.  Perhaps a good 
> interpretation of usage is an unqualified name that denotes a non-static 
> member of the enclosing class.

Perhaps it could be clarified.

> How does this play with imports?  Normal imports can import the simple name of 
> a non-static class.  So, in a compilation unit that imported the name of the 
> non-static class, does the use of that simple name in the static nested class 
> count as the imported name (legal) or the enclosing class's name (illegal)? 

Use the name lookup rules to see where the name was found.

> And what about classes in scope because they are declared in the same package? 

They are top level, so none of this applies.

>   I guess another way to word this question is: Does a non-static member type 
> shadow the imported and package-level types otherwise in the scope of another 
> static member type in the same enclosing class?

The name lookup rules are clear: it does.

> package q;
> class B {}
> 
> package q;
> import p.A;
> import q.G.H;
> class G {
>    class H {}
>    class A {}
>    class B {}
>    static class I {
>      A a; // legal? Is this imported p.A, or enclosing q.G.A?
>      B b; // legal? Is this accessible q.B, or illegal enclosing q.G.B?
>      H h; // legal? Either way, it is attempting to refer to q.G.H
>    }
> }

In all three cases, the name from the enclosing scope shadows the one from the 
import or the current package.  All three are forbidden by 8.5.2.  However, 
cases like these will be diagnosed as only a warning by javac for backward 
compatibility.  To qualify for a warning instead of an error, the following 
conditions must be satisfied
   o the member being accessed is a type name, and
   o either (a) the class from which the name is being accessed is not
     generic, OR (b) the class being accessed was defined inside a non-generic
     class.

> Also, in an above example, E imported B to extend it by simple name, but the 
> import statement currently requires that A be non-parameterized, so E is 
> forced to use the raw version of A.  Do you want to consider allowing the 
> import of a parameterized type?

No.


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com.  Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/5cFolB/TM
---------------------------------------------------------------------~->

To unsubscribe from this mailing list, send an email to:
[email protected]
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/java-spec-report/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/