Posted on Leave a comment

Using Packages – Access Control

Using Packages

The import facility in Java makes it easier to use the contents of packages. This subsection discusses importing reference types and static members of reference types from packages.

Importing Reference Types

The accessibility of types (classes, interfaces, and enums) in a package determines their access from other packages. Given a reference type that is accessible from outside a package, the reference type can be accessed in two ways. One way is to use the fully qualified name of the type. However, writing long names can become tedious. The second way is to use the import declaration that provides a shorthand notation for specifying the name of the type, often called type import.

The import declarations must be the first statement after any package declaration in a source file. The simple form of the import declaration has the following syntax:

Click here to view code image

import
fully_qualified_type_name
;

This is called single-type-import. As the name implies, such an import declaration provides a shorthand notation for a single type. The simple name of the type (i.e., its identifier) can now be used to access this particular type. Given the import declaration

Click here to view code image

import wizard.pandorasbox.Clown;

the simple name Clown can be used in the source file to refer to this class.

Alternatively, the following form of the import declaration can be used:

Click here to view code image

import
fully_qualified_package_name
.*;

This is called type-import-on-demand. It allows any type from the specified package to be accessed by its simple name. Given the import declaration

Click here to view code image

import wizard.pandorasbox.*;

the classes Clown and LovePotion and the interface Magic that are in the package wizard.pandorasbox can be accessed by their simple name in the source file.

An import declaration does not recursively import subpackages, as such nested packages are autonomous packages. The declaration also does not result in inclusion of the source code of the types; rather, it simply imports type names (i.e., it makes type names available to the code in a compilation unit).

All compilation units implicitly import the java.lang package (ยง8.1, p. 425). This is the reason why we can refer to the class String by its simple name, and need not use its fully qualified name java.lang.String all the time.

Import statements are not present in the compiled code, as all type names in the source code are replaced with their fully qualified names by the compiler.

Example 6.1 shows several usages of the import statement. Here we will draw attention to the class Baldness in the file Baldness.java. This class relies on two classes that have the same simple name LovePotion but are in different packages: wizard.pandorasbox and wizard.spells. To distinguish between the two classes, we can use their fully qualified names. However, since one of them is in the same package as the class Baldness, it is enough to fully qualify the class from the other package. This solution is used in Example 6.1 at (3). Note that the import of the wizard.pandorasbox package at (1) becomes redundant. Such name conflicts can usually be resolved by using variations of the import declaration together with fully qualified names.

The class Baldness extends the class Ailment, which is in the subpackage artifacts of the wizard.pandorasbox package. The import declaration at (2) is used to import the types from the subpackage artifacts.

The following example shows how a single-type-import declaration can be used to disambiguate a type name when access to the type is ambiguous by its simple name. The following import statement allows the simple name List to be used as shorthand for the java.awt.List type as expected:

Click here to view code image

import java.awt.*;           // imports all reference types from java.awt

Given the two import declarations

Click here to view code image

import java.awt.*;           // imports all type names from java.awt
import java.util.*;          // imports all type names from java.util

the simple name List is now ambiguous because both the types java.util.List and java.awt.List match.

Adding a single-type-import declaration for the java.awt.List type allows the simple name List to be used as a shorthand notation for this type:

Click here to view code image import java.awt.*;           // imports all type names from java.awt
import java.util.*;          // imports all type names from java.util
import java.awt.List;        // imports the type List from java.awt explicitly

Leave a Reply

Your email address will not be published. Required fields are marked *