|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 4
| Tip |
|---|
Just as you cannot store both a file and a directory with identical names in the same directory, you cannot store a class
or interface and a package with identical names in the same package. For example, given a package named accounts, you cannot store both a package and a class named payable in accounts. To avoid conflicting names, uppercase the first letter of class and interface names, and lowercase the first letter of package
names. Using the previous example, store class Payable in package accounts as accounts.Payable and package payable in package accounts as accounts.payable. Learn more about this and other naming conventions from Sun's Code Conventions for the Java Programming Language.
|
Every source file's classes and interfaces organize into a package. In the package directive's absence, those classes and interfaces belong to the unnamed package (the directory the JVM regards as the current
directory—the directory where a Java program begins its execution via the Windows java.exe, or OS-equivalent, program—and contains no subpackages). But if the package directive appears in a source file, that directive names the package for those classes and interfaces. Use the following
syntax to specify a package directive in source code:
'package' packageName [ '.' subpackageName ... ] ';'
A package directive begins with the package keyword. An identifier that names a package, packageName, immediately follows. If classes and interfaces are to appear in a subpackage (at some level) within packageName, one or more period-separated subpackageName identifiers appear after packageName. The following code fragment presents a pair of package directives:
package game; package game.devices;
The first package directive identifies a package named game. All classes and interfaces appearing in that directive's source file organize in the game package. The second package directive identifies a subpackage named devices, which resides in a package named game. All classes and interfaces appearing in that directive's source file organize in the game package's devices subpackage. If a JVM implementation maps package names to directory names, game.devices maps to a game\devices directory hierarchy under Windows and a game/devices directory hierarchy under Linux or Solaris.
| Caution |
|---|
Only one package directive can appear in a source file. Furthermore, the package directive must be the first code (apart from comments) in that file. Violating either rule causes Java's compiler to report
an error.
|
To help you get comfortable with packages, I've prepared an example that spans all topics in this article. In this section, you learn how to create the example's package. In later sections, you will learn how to import a class and an interface from this package, how to move this package to another location on your hard drive and still access the package from a program, and how to store the package in a jar file. Listing 1 presents the package's source code: