Java Reference
In-Depth Information
If you are using a Windows machine, you can set the CLASSPATH variable by set-
ting or creating an environment variable named
CLASSPATH
using the Control Panel.
In this topic, we are assuming that class path directories are separated by a semi-
colon. That is common, but some operating systems use some other separator, such as
a colon. Check your documentation if a semicolon does not work as a separator.
Package Names and the CLASSPATH Variable
A package name must be a path name for the directory that contains the classes in the
package, but the package name uses dots in place of
\
or
/
(whichever your operating sys-
tem uses). When naming the package, you use a relative path name that starts from any
directory listed in the value of the CLASSPATH (environment) variable.
EXAMPLES
utilities.numericstuff
java.util
PITFALL: Subdirectories Are Not Automatically Imported
Suppose you have two packages,
utilities.numericstuff
and
utilities.
numericstuff.statistical
. In this case, you know that
utilities.numericstuff.
statistical
is in a subdirectory (subfolder) of the directory (folder) containing
utilities.numericstuff
. This leads some programmers to assume that the follow-
ing
import
statement imports both packages:
import
utilities.numericstuff.*;
This is not true. When you import an entire package, you do not import subdirec-
tory packages.
To import all classes in both of these packages, you need
import
utilities.numericstuff.*;
■
import
utilities.numericstuff.statistical.*;
The Default Package
All the classes in your current directory (that do not belong to some other package)
belong to an unnamed package called the
default package
. As long as the current
directory is on your CLASSPATH, all the classes in the default package are automati-
cally available to your code. This is why we have always assumed that all the classes we
defined are in the same directory. That way, we need not clutter our discussion with
concern about
import
statements.
default
package