Previous | Next | Trail Map | The Java Development Environment | Managing Your Programming Environment


Roll Your Own Packages

Packages are groups of related classes and interfaces and provide convenient a mechanism for managing a large set of classes and interfaces and avoiding naming conflicts. Several packages are included as part of the Java development environment: the Java language packages, the browser packages, and the window toolkit package just to name a few.

In addition to the Java packages, you can create your own packages using Java's package statement. Suppose that you had a group of classes that implement a collection of graphic objects such as circles, rectangles, lines, and points. In addition to these classes you also defined an interface Draggable [PENDING: does this make sense?] It makes sense to collect all of these classes and interfaces together into a package called "Graphics". In this way other programmers can easily determine what the group of classes are for and how they relate to one another and to other classes and packages.

You create a package implicitly when you put a class or classes in it. Here's an outline of the Graphics package described in the previous paragraph.

package Graphics;

class Circle {
    . . .
}
The first line in the preceding code snippet indicates that all of the classes defined in this compilation unit are members of a package named Graphics. The package statement refers to everything in the current compilation unit (in most cases a compilation unit is a file). You can define a class per file and have each class be in the same package.

All classes and interfaces are in a package. Default package.


Previous | Next | Trail Map | The Java Development Environment | Managing Your Programming Environment