Java Reference
In-Depth Information
In this code, the class
Point
is declared in a compilation unit with no
package
statement,
and thus
Point
is its fully qualified name, whereas in the code:
package vista;
class Point { int x, y; }
the fully qualified name of the class
Point
is
vista.Point
. (The package name
vista
is suit-
able for local or personal use; if the package were intended to be widely distributed, it
would be better to give it a unique package name (§
6.1
).)
An implementation of the Java SE platform must keep track of types within packages by
names to make sure that such names are understood as referring to the same type.
For example, if a compilation unit contains the single-type-import declaration
import java.util.Vector;
then within that compilation unit the simple name
Vector
and the fully qualified name
java.util.Vector
refer to the same type.
If and only if packages are stored in a file system (§
7.2
), the host system may choose to
enforce the restriction that it is a compile-time error if a type is not found in a file under
a name composed of the type name plus an extension (such as
.java
or
.jav
) if either of the
following is true:
• The type is referred to by code in other compilation units of the package in which
the type is declared.
• The type is declared
public
(and therefore is potentially accessible from code in oth-
er packages).
This restriction implies that there must be at most one such type per compilation unit.
This restriction makes it easy for a Java compiler to find a named class within a pack-
age. In practice, many programmers choose to put each class or interface type in its
own compilation unit, whether or not it is
public
or is referred to by code in other com-
pilation units.
For example, the source code for a
public
type
wet.sprocket.Toad
would be found in a
file
Toad.java
in the directory
wet/sprocket
, and the corresponding object code would be
found in the file
Toad.class
in the same directory.