Java Reference
In-Depth Information
The java.lang package is the only package imported automatically without
an explicit command; all other packages need an import statement to tell the
compiler where to access the classes, fields, and methods of an existing class in the
package. The import statement is placed at the beginning of the Java source code,
most commonly right after the opening documentation. During compilation, the
import statement goes to the location where the SDK is stored, and loads the
appropriate class or classes. Typing an asterisk (*) after the package name tells the
program to load all the classes within a package; typing an individual class name
after the package name tells the program to load an individual class.
Using the import Statement
Using the import statement to import all of the classes in a
package does not make the bytecode longer because the compiler
uses only the classes it needs. Importing numerous packages does
slow the compiler, however, as it checks through each package
when it needs a class. To keep compile times reasonable, a pro-
grammer should use the import statement to import only pack-
ages with the tools the compiler needs.
In the Welcome to My Day application, you will import the Date class from
the java.util package, as shown in the following steps.
To Code the Import Statement
1. With the Welcome.java file displayed in the TextPad coding window,
click line 9, below the block comment. Press the ENTER key.
The insertion point displays in line 10 (Figure 2-34).
block
comment
insertion
point
FIGURE 2-34
(continued)
 
Search WWH ::




Custom Search