Java Reference
In-Depth Information
Code in a package imports the rest of its own package implicitly, so
everything defined in a package is available to all types in the same
package. The package java.lang is implicitly imported in all code.
The import mechanism is passive in that it does not cause information
about the named packages and types to be read in at compile timeun-
less a type is used. The import statement simply tells the compiler how
it can determine the fully qualified name for a type that is used in your
program if it can't find that type defined locally. For example, as de-
scribed on page 181 , if your class declares a reference of type Attributed
the compiler will search for the type in the following order:
1. The current type including inherited types.
2. A nested type of the current type.
3. Explicitly named imported types (single type import).
4. Other types declared in the same package.
5. Implicitly named imported types (import on demand).
If, after that, the type is still not found it is an error. It is also an error
if any package named in an import statement cannot be locatedeven if
there is no actual need to obtain a type from that package.
Resolving which type is being referred to requires some work by the
compiler, and use of import on demand may involve more work than
resolving a single type import. However, this is rarely significant in the
compilation process. The choice between a single type import and im-
port on demand is usually a matter of personal preference or local style
rules. Naturally, to resolve conflicts or to ensure that a specific type is
resolved correctly you may need to use a single type import. [1]
[1] For example, if you use import on demand for a given package and there is a type in that package
with the same name as a type in the current package, then the current package type would be found
ahead of the imported package. In this case you could use a single type import for that one type to
make it clear that it is the type from the other package that is required. Alternatively, of course, you
could just use the fully qualified name.
 
 
Search WWH ::




Custom Search