The package statement identifies the package in which a source file's types are located. This statement consists of reserved word package, followed by a member access operator-separated list of package and subpackage names, followed by a semicolon.
For example, package graphics; specifies that the source file's types locate in a package named graphics, and package graphics.shapes; specifies that the source file's types locate in the graphics package's shapes subpackage.
By convention, a package name is expressed in lowercase. If the name consists of multiple words, each word except for the first word is capitalized.
Only one package statement can appear in a source file. When it is present, nothing apart from comments must precede this statement.
CAUTION: Specifying multiple package statements in a source file or placing anything apart from comments above a package statement causes the compiler to report an error.
Java implementations map package and subpackage names to same-named directories. For example, an implementation would map graphics to a directory named graphics, and would map graphics.shapes to a shapes subdirectory of graphics. The Java compiler stores the classfiles that implement the package's types in the corresponding directory.
NOTE: If a source file does not contain a package statement, the source file's types are said to belong to the unnamed package. This package corresponds to the current directory.
Was this article helpful?