Java Reference
In-Depth Information
This package-naming convention is so widely used in the Java industry as to be
completely standardized.
Packages can be nested to any depth. In most situations the first two or more
names reflect the domain name of the vendor or creator of the package. Many
open source packages exist as well, with package names like
org.apache.ant
,
for example. Another example is the CORBA package (see Chapter 19). CORBA
is maintained by the Open Management Group, a standards body with the internet
domain name
omg.org
(see
www.omg.org
for much more information about
the OMG). Thus all the CORBA classes live in package structures that begin
with
org.omg.CORBA
,
org.omg.Messaging
, etc. As mentioned above, these
are the only packages in the entire Java class library that use uppercase letters
anywhere in the package names.
5.3.2 Import
Unless special arrangements are made, all classes must be referred to by their
complete name, which includes the full package name (such names are called
“fully qualified” names). Therefore, in
TestABC.java
above we specified the
full package name of the
TestC
class in the declaration and
new
expression:
mypack.extrapack.TestC testc
=
new mypack.extrapack.TestC ();
An exception to this rule holds for all the classes in the standard
java.lang
package. Thus, whenever we use the
Float
class, for instance, we merely use
something like
Float someFloat = new Float (3.14);
There are nearly 3000 classes in the 135 packages in the J2SE 1.4.2 standard
class library (over 3000 classes in 165 packages in J2SE 5.0). Commercial class
libraries that one might use add many more packages. Using fully qualified class
names obviously becomes unwieldy in any non-trivial program since such a
program will involve many different classes from many different packages.
Thankfully, the Java compiler provides the special arrangements necessary
to abbreviate most fully qualified class names with just the short class name by
using the
import
directive. That is, the compiler understands a shortcut that
allows us to refer to something like
java.awt.event.ActionEvent
(see
Chapter 7) as simply
ActionEvent
. The appearance of an
import
directive
tells the compiler where to look for class definitions when it comes upon a short
class name that is not fully qualified. Every class in the
java.lang
package is
imported automatically. That's why we can refer to the
java.lang.Float
class
as just
Float
.Touse this abbreviation technique for classes in other packages
requires that the class name or package name appear in an
import
statement.
As an example in a class definition for an applet class, this
import
directive:
import java.applet.Applet;
Search WWH ::
Custom Search