Java Reference
In-Depth Information
Single-Type-Import vs. Type-Import-On-Demand Declarations
Lines 3-4 of Fig. 21.5 are single-type-import declarations —they each specify one class to
import. When a source-code file uses multiple classes from a package, you can import those
classes with a a type-import-on-demand declaration of the form
import packagename .*;
which uses an asterisk ( * ) at its end to inform the compiler that all public classes from the
packagename package can be used in the file containing the import . Only those classes that
are used are loaded at execution time. The preceding import allows you to use the simple
name of any type from the packagename package. Throughout this topic, we provide
single-type-import declarations as a form of documentation to show you specifically which
types are used in each program.
Common Programming Error 21.1
Using the import declaration import java.*; causes a compilation error. You must spec-
ify the full package name from which you want to import classes.
Error-Prevention Tip 21.1
Using single-type-import declarations helps avoid naming conflicts by importing only the
types you actually use in your code.
Specifying the Classpath When Compiling a Program
When compiling ListTest , javac must locate the .class files for classes List and Emp-
tyListException to ensure that class ListTest uses them correctly. The compiler uses a
special object called a class loader to locate the classes it needs. The class loader begins by
searching the standard Java classes that are bundled with the JDK. Then it searches for op-
tional packages . Java provides an extension mechanism that enables new (optional) pack-
ages to be added to Java for development and execution purposes. If the class is not found
in the standard Java classes or in the extension classes, the class loader searches the class-
path —a list of directories or archive files containing reusable types. Each directory or ar-
chive file is separated from the next by a directory separator —a semicolon ( ; ) on Windows
or a colon ( : ) on UNIX/Linux/Mac OS X. Archive files are individual files that contain di-
rectories of other files, typically in a compressed format. For example, the standard classes
used by your programs are contained in the archive file rt.jar , which is installed with the
JDK. Archive files normally end with the .jar or .zip file-name extensions.
By default, the classpath consists only of the current directory. However, the classpath
can be modified by
1. providing the -classpath listOfDirectories option to the javac compiler or
2. setting the CLASSPATH environment variable (a special variable that you define
and the operating system maintains so that programs can search for classes in the
specified locations).
If you compile ListTest.java without specifying the -classpath option, as in
javac ListTest.java
the class loader assumes that the additional package(s) used by the ListTest program are
in the current directory . As we mentioned, we placed our package in the parent directory so
 
Search WWH ::




Custom Search