Java Reference
In-Depth Information
UNDERSTANDING PACKAGES
Packages are implicit in the organization of the standard classes as well as your own programs, and they in-
fluence the names you can use for classes and the variables and methods they contain. Essentially, a
package
is a uniquely named collection of classes. The primary reason for grouping classes in packages is to avoid
possible name clashes with your own classes when you are using prewritten classes in an application. The
names used for classes in one package do not interfere with the names of classes in another package or your
program because the class names in a package are all qualified by the package name. Thus, the
String
class
you have been using is in the
java.lan
g package, so the full name of the class is
java.lang.String
. You
have been able to use the unqualified name because all the classes in the
java.lang
package are always
available in your program code; there's an implicit
import
statement in effect for all the names in the
java.lang
package. If you happened to have defined a class of your own with the name
String
, using the
name
String
would refer to your class, but you could still use the library class that has the same name by
using its full name in your code,
java.lang.String
.
Every class in Java is contained in a package, including all those you have defined in the examples. You
haven't seen many references to package names so far because you have been implicitly using the
default
package
to hold your classes, and this doesn't have a name.
All of the standard classes in Java are contained within a set of packages, and each package contains
classes that are related in some way. The package that contains most of the standard classes that you have
used so far is called
java.lang
, so called because the classes in this package provide Java language-related
support. You haven't seen any explicit reference to
java.lang
in your code either, because this package
is automatically available to your programs. Things are arranged this way because some of the classes in
java.lang
, such as
String
, are used in every program. If you use a class from the other packages contain-
ing standard classes, you need either to use the fully qualified name of the class or to explicitly import the
full class name into your program.
Packaging Up Your Classes
Putting one of your classes in a named package is very simple. You just add a package statement as the first
statement in the source file containing the class definition. Note that it must always be the
first
statement.
Only comments or blank lines are allowed to precede the package statement. A
package statement
consists
of the keyword
package
followed by the package name and is terminated by a semicolon. If you want the
classes in a package to be accessible outside the package, you must declare the class using the keyword
pub-
lic
in the first line of your class definition. Class definitions that aren't preceded by the keyword
public
are accessible only from methods in classes that belong to the same package.
For example, to include the
Line
class in a package called
Geometry
, the contents of the file
Line.java
needs to be:
package Geometry;
public class Line {
// Details of the class definition
}
Each class that you want to include in the package
Geometry
must contain the same package statement
at the beginning, and you must save all the files for the classes in the package in a directory with the same