Defining a Package

Purpose:

To declared that a file defines classes or interfaces which are part of a cohesive unit.

Mechanics:

package <package name>;

Example:

package Shapes;

Usage:

  • <package name>is any valid identifier.
  • All files in the same package must use the same <package name>.
  • <package name> usually must also reflect the directory structure in which the files are stored; that is, if a package is stored in the CLASSPATH/Cloud, <package name> should be Cloud.
  • Classes and interfaces defined in the same package have acces to each other's protected, as well as public, methods and instance variables. they also have access to "package-friendly" classes, interfaces, methods, and instance variables. "Package-friendly" classes, interfaces, methods, and instance variables are those that are declared without a public, protected, or private modifier.

Restrictions:

The package statement must appear before any other Java statements in the file, or be omitted entirely. If the package statement is omitted, then a file is assumed to be of the default package of which all files that do not declare a package are part.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu