4.2.1 Packages


Related classes can be bundled together in a collection called a package. The main advantages of using a package are that it helps you to organize your work because
  1. Using a Package
  2. Creating a Package
  3. CLASSPATH
  4. Making an Archive of Packages

Using a Package

Every Java class is contained in one or another package. The built-in packages that you use most frequently are java.lang, java.applet, and java.awt. The first one is so basic that the Java compiler and the Java interpreter import it automatically so that all classes inside are available under their abbreviated names (e.g. you can immediately use String instead of java.lang.String). The publicly accessible classes of the other packages must be imported explicitly unless you are happy to use full names. There are three ways to import a public class :
1. Importing a Single Class
The statement
import java.awt.Graphics;
imports the class Graphics from the java.awt package. After this statement, the signature of the paint method of an applet can be defined by
public void paint(Graphics g){ }
instead of by
public void paint(java.awt.Graphics g){ }
2. Importing All Classes
If you want to import all the classes and interfaces from a package, for instance, the entire java.awt package, use the import statement with the asterisk (*) wildcard character.
import java.awt.*;
Importing all classes of a package may slow down compilation, but has no other effects on efficiency.
3. Allowing Short names
Without the wild card extension in the previous import statement, you only allow abbreviation of class names. For example
import java.awt;
allows you to use the name image.ColorModel instead of java.awt.image.ColorModel.

Creating a Package

The best way to explain how to create a package is by example. Suppose that you are implementing a group of classes that represent a collection of geometrical shapes such as ellipses, rectangles, triangles, and so on. Suppose that you will use interfaces such as Blinkable and Draggable that classes implement if they can be blinking on the screen or if they can be dragged with the mouse by the user. Assuming that you wish to make your collection of classes and interfaces available to other users in bundled form, say in the package called shapes, then Three remarks:
  1. If a user has imported both the shapes package and the java.awt package, then he or she can distinguish between the two Rectangle classes present in both packages by using the larger names shapes.Rectangle and awt.Rectangle, respectively.
  2. Your package names can have multiple components separated by periods. For example you may choose to have the packages geometry.shapes and geometry.constructions, that contains classes and interfaces for describing geometrical objects and for manipulating geometrical objects, respectively. Each component of the package name represents a directory on the file system. In our example, the directory geometry would have two subdirectories, shapes and constructions, respectively.
  3. The Java designers have proposed package naming based on Internet domain names. Suppose that the domain name of your organization is wins.uva.nl and suppose that your organization divides the package name space internally in such a way that your part is called classes, then your packages would have full names like NL.uva.wins.classes.packageName. For example, the following name fits in this scheme:

CLASSPATH

The CLASSPATH is a list of names of directories separated by colons (semicolons) on a Unix System (Windows system). When the Java compiler or the Java interpreter get a classname, they search each directory in the CLASSPATH until they find the class they are looking for. On a Unix system you might use the following specification of the CLASSPATH environment variable:
setenv CLASSPATH .:~/classes:/usr/local/java/lib
On a Windows or DOS system, you might use the following specification of the CLASSPATH environment variable:
SET CLASSPATH=C:\JAVA\LIB;.;C:\CLASSES

Making an Archive of Packages

If you look at the standard location where Java stores the built-in classfiles, normally a directory whose name ends with java/lib, you will not find individual subdirectories with names like awt and util, but instead a single ZIP file, classes.zip. If you look inside the archive with a ZIP viewer, you will see the paths and file names that you expect. If you like, you can also make a ZIP-archive of your packages, in a file called classes.zip, which is located in the CLASSPATH.