Java package

This is an old revision of this page, as edited by Doug Bell (talk | contribs) at 01:56, 12 March 2006 (moved Packages in Java to Java package: Better name.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A Java package is a Java programming language mechanism for organizing classes into namespaces. Java source files belonging to the same category or providing similar functionality can include a package statement at the top of the file to designate the package for the classes the source file defines. Java packages can be stored in compressed files called JAR files.

Using packages

To use a package inside a Java source file, it is convenient to import the classes from the package with an import statement. The statement

import java.awt.event.*;

imports all classes from the java.awt.event package, while

import java.awt.event.ActionEvent;

imports only the ActionEvent class from the package. After either of these import statements, the ActionEvent class can be referenced using its simple class name:

ActionEvent myEvent = new ActionEvent();

Classes can also be used directly without an import statement by using the fully-qualified name of the class. For example,

java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();

doesn't require a preceding import statement.

Creation of JAR Files

In Java source files the package the file belongs to is specified with the package keyword.

package java.awt.event;

JAR Files are created with the jar command-line utility. The command

jar cf myPackage.jar *.class

compresses all *.class files into the JAR file myPackage.jar.

Package naming conventions

Packages are usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods (.) (pronounced "dot"). Although packages lower in the naming hierarchy are often referred to a "subpackages" of the corresponding packages higher in the hierarchy, there is no semantic relationship between packages. The Java Language Specification establishes package naming conventions in order to avoid the possibility of two published packages having the same name. The naming conventions describe how to create unique package names, so that packages that are widely distributed will have unique namespaces. This allows packages to be easily and automatically installed and catalogued.

In general, a package name begins with the top level ___domain name of the organization and then the organization's ___domain and then any subdomains listed in reverse order. The organization can then choose a specific name for their pacakge. Package names should be all lowercase characters whenever possible.

For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a US company named MySoft also creates a fractions package, but names it com.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace.

Complete conventions for disambiguating package names and rules for naming packages when the Internet ___domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.

Core packages in J2SE 5.0

See also

JAR (file format)