Packages

This tutorial is an abbreviated version of http://java.sun.com/docs/books/tutorial/java/interpack/packages.html.

Programmers bundle groups of related classes and interfaces into packages to:


Definition:  A package is a collection of related classes and interfaces providing access protection and namespace management.

The classes and interfaces that are part of the Java platform are members of various packages that bundle classes by function:

Why put them in a package?

Suppose that you write:
//in the Graphic.java file
public abstract class Graphic {
. . .
}

//in the Circle.java file
public class Circle extends Graphic implements Draggable {
. . .
}

//in the Rectangle.java file
public class Rectangle extends Graphic implements Draggable {
. . .
}

//in the Draggable.java file
public interface Draggable {
. . .
}

You should bundle these classes and the interface in a package for several reasons:

Creating a Package

package graphics;
public class Circle extends Graphic implements Draggable {
. . .
}
package graphics;

public class Rectangle extends Graphic implements Draggable {
. . .
}
All classes and interfaces defined in Circle.java and Rectangle.java are also members of the graphics package.

Naming a Package

This works just fine unless 2 independent programmers use the same name for their packages. What prevents this problem? Convention.


 Use <team name>.<project name> as the top 2 components of your project's package name.

Using Package Members

Each is appropriate for different situations, as explained in the following sections.

Referring to a Package Member by Name

This is the qualified name for the Rectangle class declared in the graphics package in the previous example:
graphics.Rectangle
You could use this long name to create an instance of graphics.Rectangle:
graphics.Rectangle myRect = new graphics.Rectangle();

This sometimes is done for 1-time use of a member. For example,

public class FancyObject implements java.io.Serializable { ... }

Importing a Package Member

To import the Circle class from the graphics package created in the previous section:
import graphics.Circle;
Now you can refer to the Circle class by its simple name:
Circle myCircle = new Circle();

Importing an Entire Package

To import all the classes and interfaces contained in a particular package, use the import statement with the asterisk (*) wildcard character:
import graphics.*;
Now you can refer to any class or interface in the graphics package by its short name:
Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();
The asterisk in the import statement can be used only to specify all the classes within a package.

It cannot be used to match a subset of the classes in a package. For example, the following does not match all the classes in the graphics package that begin with A:

import graphics.A*;	// does not work
The * is not recursive: It does not import  any classes in subpackages.

Java runtime system automatically imports the java.lang package.

Disambiguating a Name

Rectangle rect;
In such a situation, use the member's qualified name to indicate exactly which Rectangle class you want:
graphics.Rectangle rect;

Managing Source and Class Files

The qualified name of the package member and the path name to the file are parallel, assuming the UNIX file name separator slash (/):

class name graphics.Rectangle
pathname to file graphics/Rectangle.java

You thus can give the classes directory to your customoers without [readily] revealing your source code.


Definition:  A class path is an ordered list of directories or ZIP files in which to search for class files.

Summary of Creating and Using Packages

To use a class or an interface that's in a different package, you have 3 choices:

  1. Use the fully qualified name of the class or the interface.
  2. Import the class or the interface.
  3. Import the entire package of which the class or the interface is a member.


In this course, set your class path so the compiler and interpreter can find your source and class files for your classes and interfaces.