|
|
|
|||
|
Importing Classes and PackagesThe first two lines of the following listing import two classes used in the applet:Applet and Graphics.
If you removed the first two lines, the applet could still compile and run, but only if you changed the rest of the code like this:
As you can see, importing the Applet and Graphics classes
lets the program refer to them later without any prefixes.
The java.applet. and java.awt. prefixes
tell the compiler which packages
it should search for the Applet and Graphics classes.
Both the java.applet and java.awt packages
are part of the core Java API --
API that every Java program can count on being in the Java environment.
The java.applet package contains classes
that are essential to Java applets.
The java.awt package contains the most frequently used classes
in the Abstract Window Toolkit (AWT),
which provides the Java graphical user interface (GUI).
You might have noticed that the
"Hello World" application Besides importing individual classes, you can also import entire packages. Here's an example:
In the Java language, every class is in a package.
If the source code for a class
doesn't have a package statement at the top,
declaring the package the class is in,
then the class is in the default package.
Almost all of the example classes in this tutorial
are in the default package.
See
Creating and Using Packagespackage statement.
Within a package,
all classes can refer to each other without prefixes.
For example, the | |||
|