Java Reference
In-Depth Information
To make a package, group all the classes together into a single directory (folder) and
add the following
package
statement to the beginning of each class file:
package
Package_Name;
This
package
statement should be at the beginning of the file. Only blank lines and
comments may precede the
package
statement. If there are both
import
statements
and
package
statements, any
package
statements come before the
import
statements.
Aside from the addition of the
package
statement, class files are just as we have already
described them. (It is technically only the
.class
files that must be in the package
directory.)
The Package
java.lang
The package
java.lang
contains classes that are fundamental to Java programming.
These classes are so basic that the package is always imported automatically. Any class
in
java.lang
does not need an
import
statement to make it available to your code.
For example, the classes
Math
and
String
and the wrapper classes introduced earlier in
this chapter are all in the package
java.lang
.
Package
A
package
is a collection of classes that have been grouped together into a directory and
given a package name. The classes in the package are each placed in a separate file, and the
file is given the same name as the class, just as we have been doing all along. Each file in
the package must have the following at the beginning of the file. Only comments and blank
lines may precede this package statement.
SYNTAX
package
Package_Name
;
EXAMPLES
package
utilities.numericstuff;
package
java.util;
Package Names and Directories
A package name is not an arbitrary identifier. It is a form of path name to the directory
containing the classes in the package.
In order to find the directory for a package, Java needs two things: the name of the
package and the value of your
CLASSPATH variable
.
You should already be familiar with the environment variable of your operating
system that is known as the
PATH variable
. The
CLASSPATH variable
is a similar
environment variable used to help locate Java packages. The value of your CLASSPATH
CLASSPATH
variable