PACKAGES(2)
PACKAGES(2)
IMPORT DECLARATIONS:
An import declaration allows a static member (in another type) or a named type (in another package) to be referred to by a simple name that consists of a single identifier. The appropriate word, import, is used in the declaration.
There are four types of import declarations:
1. single-type-import declaration (imports a single named type by mentioning its canonical name.)
2. type-import-on-demand declaration (imports all the accessible types of a named type as needed.)
3. single-static-import declaration (imports all accessible static members with a given name from a type, by giving its canonical name.)
4. static-import-on-demand declaration (imports all accessible static members of a named type as needed.)
the scope of any type imported is all the class and interface type declaration in the compilation unit in which the import declaration appears. It does not include, in its scope:
1. the package statement
2. other import declarations in the current compilation unit
3. other compilation units in the same package.
Single-type-import declaration: a single-type-import declaration imports a single type by giving its canonical name, making it available under a simple name in the class and interface declaration in the compilation unit in which the single-type-import declaration appears.
Denoted in this : import TypeName ;
Given a single-type-import declaration, d, the type imported, n, in a compilation unit, c, of package , p, shadows:
- any top level same-named type, d, in another compilation unit of p throughout c.
- any type with same name, n, imported by a type-import-on-demand declaration in c throughout c.
- any type with same name, n, imported by a static-import-on-demand declaration in c throughout c.
Type-import-on-demand declaration: a type import-on-demand declaration allows all accessible types declared in the type or package named by a canonical name to be imported as needed.
Denoted as: import PackageOrTypeName.* ;
Note:
1. the type or package must be accessible or a compile time error is the result.
2. when redundancy in importing is encountered, i.e where 2 or more type import-on-demand declaration names the same type or package, the effect is as if that type were imported only once.
3. if a type-import-on demand declaration and a static-import-on-demand declaration both import the same type in a compilation unit, the effect is as if the static member type of that type were imported only once.
4. a type import on demand declaration will never shadow any other declaration.
Considering a type imported as: import java.util.* ; into the class or interface type of this compilation unit,
5. a simple name Vector refers to a type Vector in java.util.Vector, provided Vector is a. not shadowed, b. obscured in its scope.
6. a single-type-import declaration with simple name Vector might shadow, java.util.Vector; an explicit declaration of a type Vector in the class or interface might shadow java.util.Vector; a nested class or interface with simple name Vector might also shadow it.
7. declaration of a field, parameter or local variable named Vector might obscure java.util.Vector.
Single static import declaration: a single-static-import declaration imports all accessible static members with a given simple name from a type.
Denoted as: import static TypeName.identifier;
The TypeName must be the canonical name of a class or interface type which exists and is accessible, otherwise a compile time error is the result.
The identifier must name at least one existing static member of the named type which is accessible, otherwise a compile time error occurs.
Note:
1. a static member, n, whether field or method name imported this way will shadow every other static-import-on-demand member n whether field or method with the same signature as the single static member.
2. any static type with the same name n imported by a static-import-on-demand declaration is also shadowed i.e
o any static type with same name n imported by static-import-on-demand declaration throughout the compilation unit.
o Any top level type named n declared in another compilation unit throughout the compilation unit
o Any type named n imported in a type import on demand declaration in the compilation unit throughout the compilation unit.
3. redundancy is allowed.
4. if the simple name of a member and a type imported by a single-static-import declaration and a single-type-import declaration respectively, a compile time error occurs.
5. if a single-static-import declaration imports a type n and the compilation unit explicitly declares same type n, a compile time error results.
Static import on demand declaration: a static-import-on-demand declaration allows all accessible type members declared in the type named by a canonical name to be imported as needed.
Denoted as: import static TypeName.* ;
Note:
1. the type used for importing must exist.
2. redundancy is allowed, either in types or members.
3. if a compilation unit contains a static-import-on-demand declaration and a type-import-on-demand declaration that name the same type, the effect is as if the static member types of that type were imported only once.
Automatic imports: all the public type names declared in the predefined package, java.lang are automatically imported.
No comments:
Post a Comment