Static import from default package ?

Dan Smith daniel.smith at
Wed Mar 28 12:38:54 PDT 2012

I looked for some clarification from the spec.  I think the problem is that A is not in scope.

- 7.5.4: The static-import name must be a TypeName and it must be a canonical name
- The meaning of an unqualified TypeName is the matching in-scope type declaration, assuming there is exactly one
- 6.3: The scope of a top-level type is "all type declarations" (_not_ all compilation units) in the same package

This odd scoping rule allows imports to get around certain name conflicts between types and packages:

package A; public class Foo {}
public class A { String Foo = "x"; }
import A.Foo;
class Test {
  Foo f1; // ok
  A.Foo f2; // error: A.Foo is a field, not a type

I think the JLS's giving a type in an unnamed package a "canonical name" (6.7) is pointless, because you can't use that canonical name in an import due to the scoping rules, and it's not even a unique identifier, since a system can have multiple unnamed packages (7.4.2).  So I prefer to explain your example with the (apocryphal) claim that you can't refer to "A" in an import because it doesn't have a canonical name.


On Mar 28, 2012, at 8:59 AM, Ulf Zibis wrote:

> Hi all,
> is there any reason, why we can't import static from class in default package?
> Example:
> class A {
>    static final type CONSTANT = 123;
>    static final type method() {...};
> }
> import static A.*;
> class B {
>    static final type HERE = CONSTANT;
>    static final type now() { return method(); };
> }
> Use case:
> JDK's jtreg tests (based on default package).
> Regards,
> Ulf

More information about the compiler-dev mailing list