Java Reference
In-Depth Information
Extensions to the Java platform that have been standardized by Oracle (or originally
Sun) typically have package names that begin with javax . Some of these extensions,
such as javax.swing and its myriad subpackages, were later adopted into the core
platform itself. Finally, the Java platform also includes several “endorsed standards,”
which have packages named after the standards body that created them, such as
org.w3c and org.omg .
a x
Every class has both a simple name, which is the name given to it in its definition,
and a fully qualified name, which includes the name of the package of which it is a
part. The String class, for example, is part of the java.lang package, so its fully
qualified name is java.lang.String .
This section explains how to place your own classes and interfaces into a package
and how to choose a package name that won't conflict with anyone else's package
name. Next, it explains how to selectively import type names or static members into
the namespace so that you don't have to type the package name of every class or
interface you use.
Package Declaration
To specify the package a class is to be part of, you use a package declaration. The
package keyword, if it appears, must be the first token of Java code (i.e., the first
thing other than comments and space) in the Java file. The keyword should be fol‐
lowed by the name of the desired package and a semicolon. Consider a Java file that
begins with this directive:
package org . apache . commons . net ;
All classes defined by this file are part of the package org.apache.commons.net .
If no package directive appears in a Java file, all classes defined in that file are part of
an unnamed default package. In this case, the qualified and unqualified names of a
class are the same.
The possibility of naming conflicts means that you should not
use the default package. As your project grows more compli‐
cated, conflicts become almost inevitable—much better to cre‐
ate packages right from the start.
Globally Unique Package Names
One of the important functions of packages is to partition the Java namespace and
prevent name collisions between classes. It is only their package names that keep the
java.util.List and java.awt.List classes distinct, for example. In order for this
to work, however, package names must themselves be distinct. As the developer of
Java, Oracle controls all package names that begin with java , javax , and sun .
Search WWH ::




Custom Search