Java Reference
In-Depth Information
Import Declarations
You learned two rules in the previous section:
public to use it in a package other than the package in which it is
You must declare a class
declared.
You need to use the fully qualified name of a class to use it in a package other than the one in
which it is declared. A class can be referred to using its simple name in the package in which it
is declared.
There is no alternative to the first rule. That is, a class must be declared public if it needs to be accessible from
outside its package.
There is another way to deal with the second rule. You can refer to a class by its simple name outside its package
by using an import declaration. An import declaration is used to import a class into a compilation unit from outside
the package of the compilation unit. Technically speaking, an import declaration is used to import any type into
a compilation unit, not just a class. Import declarations appear just after the package declaration and before the
first type declaration. Figure 6-3 shows the place where import declarations appear. You can have multiple import
declarations in a compilation unit.
A compilation unit
package declaration
import declaration 1
import declaration 2
class declaration 1
class declaration 2
other type declarations
Figure 6-3. The structure of a compilation unit in Java
This section mentions importing only class types. However, the same rules apply for importing any other types,
for example, interface types, annotation types, or enum types. Because I have covered only class types up to this point,
I will not mention any other types in this discussion.
There are two types of import declarations:
Single-type import declaration
Import-on-demand declaration
 
Search WWH ::




Custom Search