5. Packages

Groups of related classes bundled into a package. Packages can also contain interface definitions.

5.1. Advantages

  • It makes objects easier to find and use.

  • It avoids naming conflicts.

5.2.1. Definition

package graphics;
interface Draggable { . . . }
class Circle { . . . }
class Rectangle { . . . }

The .class files generated must be placed in a directory named graphics somewhere in your CLASSPATH.

CLASSPATH: A list of directories that indicate where on the file system you've installed various compiled Java classes and interfaces.

Java searches your CLASSPATH for a directory whose name matches the package name of which the class is a member.

Note: If you don't specify a package, your classes and interfaces become members of the default package, which has no name.

To use the classes and interfaces defined in one package from within another package, you need to import the package:

import graphics.Circle;
import graphics.*;

Also, the classes and interfaces must be declared public.

This packages are always automatically imported:

  • The default package (a package with no name);

  • The java.lang package.

Audio in Portuguese

5.4.1. The Java language package

It contains classes that are core to the Java language:

  • Object: The class from which all others inherit.

  • Data Type Wrappers: A collection of classes used to wrap variables of a primitive data type.

  • Strings: Two classes that implement character data.

  • System and Runtime : These two classes provide let your programs use system resources.

  • Threads: It implements the multi-threading capabilities so important to the Java language.

  • Classes: The Class class provides a runtime description of a class and the ClassLoader class allows you to load classes during runtime.

  • Math: A library of math routines and values such as pi.

  • Exceptions, Errors and Throwable: Classes that handle errors.

  • Processes: Process objects represent the system processes.

5.4.2. The Java I/O Package

The Java I/O Package provides a set of input and output streams.

5.4.3. The Java Utility Package

This Java package, java.util, contains a collection of utility classes.

5.4.4. The Java Networking Package

The java.net package implements various networking capabilities.

5.4.5. The Applet Package

This package contains the Applet class -- the class that you must subclass if you're writing an applet.

5.4.6. The Abstract Window Toolkit Packages

Three packages comprise the Abstract Window Toolkit.

Audio in Portuguese