Order of automatically imported packages and ambiguity

后端 未结 3 1874
醉话见心
醉话见心 2021-01-13 01:39

JLS: Chapter 7. Packages:

A package consists of a number of compilation units (§7.3). A compilation unit automatically has access to all types

相关标签:
3条回答
  • 2021-01-13 02:31

    Actually the issue is about simple type names:

    6.5.5.1 Simple Type Names If a type name consists of a single Identifier, then the identifier must occur in the scope of exactly one visible declaration of a type with this

    About scope we can read here:

    (6.3) The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible

    ..

    The scope of a type imported by a single-type-import declaration (§7.5.1) or a type-import-on-demand declaration (§7.5.2) is all the class and interface type declarations (§7.6) in the compilation unit in which the import declaration appears, as well as any annotations on the package declaration (if any) of the compilation unit .

    0 讨论(0)
  • 2021-01-13 02:35

    The import statements of the form:

    import packageName.subPackage.*
    

    is Type-Import-on-Demand Declarations. i.e, the classes or any types from them will be imported only when that type is not available in the scope of current compilation unit.

    From example 7.5.2-1 of that JLS section only:

    The declaration might be shadowed by a single-type-import declaration of a type whose simple name is Vector; by a type named Vector and declared in the package to which the compilation unit belongs; or any nested classes or interfaces.

    So, if you have a class String in the same package as your class, then using String in that class, will refer to your class, as java.lang.String will not be imported. It will only be imported on demand, as shown in example 6.4.1-2 of JLS § 6.4.1 - Shadowing.

    0 讨论(0)
  • 2021-01-13 02:35

    I think this is covered in Shadowing & Obscuring in that a package scoped Class takes precedence over a wildcard import.

    0 讨论(0)
提交回复
热议问题