Java Reference
In-Depth Information
Adding Classes from a Package to Your Program
You used the
import
statement frequently in examples but nonetheless I'm describing it here from the
ground up. Assuming they have been defined with the
public
keyword, you can add all or any of the classes
in a named package to the code in your program by using an
import
statement
. You can then reference the
classes that you make available to your program through the
import
statement just by using the class names.
For example, to make available all the classes in the package
Geometry.Shapes3D
to a source file, you just
need to add the following
import
statement to the beginning of the file:
import Geometry.Shapes3D.*; // Include all classes from this package
The keyword
import
is followed by the specification of what you want to import. The wildcard
*
, fol-
lowing the period after the package name, selects all the classes in the package, rather like selecting all the
files in a directory. Now you can refer to any public class in the package just by using the class name. Again,
the names of other classes in your program must be different from the names of the classes in the package.
Importing all the names in a package is not an approach you should adopt generally as it defeats the primary
objective of putting classes in packages. It's usually better to import just the names from a package that your
code references.
If you want to add a particular class rather than an entire package, you specify its name explicitly in the
import
statement:
import Geometry.Shapes3D.Sphere; // Include the class Sphere
This includes only the
Sphere
class in the source file. By using a separate
import
statement for each
individual class from the package, you ensure that your source file includes only the classes that you need.
This reduces the likelihood of name conflicts with your own classes, particularly if you are not fully familiar
with the contents of the package and it contains a large number of classes.
NOTE
The
*
canbeusedonlytoselectalltheclassesinapackage.Youcan'tuse
Geometry.*
to select all the packages in the
Geometry
directory.
Packages and Names in Your Programs
A package creates a self-contained environment for naming your classes. As I've said, this is the primary
reason for having packages in Java. You can specify the names for classes in one package without worrying
about whether the same names have been used elsewhere. Java makes this possible by treating the package
name as part of the class name — actually as a prefix. This means that the class
Sphere
in the package
Geo-
metry.Shapes3D
has the full name
Geometry.Shapes3D.Sphere
. If you don't use an
import
statement to
incorporate the class in your program, you can still make use of the class by referring to it using its full class
name. If you needed to do this with the class
Sphere
, you might declare a variable with the statement:
Geometry.Shapes3D.Sphere ball = new Geometry.Shapes3D.Sphere(10.0, 1.0, 1.0, 1.0);