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 beCloud. - Classes and interfaces defined in the same package have acces
to each other's
protected, as well aspublic, 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 apublic, protected, orprivatemodifier.
Restrictions:
- The
packagestatement must appear before any other Java statements in the file, or be omitted entirely. If thepackagestatement 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.
email suggestions to: cs015tas@cs.brown.edu
