Java Reference
In-Depth Information
As noted above, you can use any of the classes from the java.lang package in your programs by
default. To use classes from the other packages, you will typically use import statements to identify the
names of the classes that you need from each package. This will allow you to reference the classes by
the simple class name. Without an import statement you would need to specify the fully qualified
name of each class from a package each time you refer to it. As we will see in a moment, the fully
qualified name for a class includes the package name as well as the basic class name. Using fully
qualified class names would make your program code rather cumbersome, and certainly less readable. It
would also make them a lot more tedious to type in.
You can use an import statement to import the name of a single class from a package into your
program, or all the class names. The two import statements at the beginning of the code for the applet
you saw earlier in this chapter are examples of importing a single class name. The first was:
import javax.swing.JApplet;
This statement imports the JApplet class name that is defined in the javax.swing package.
Formally, the name of the JApplet class is not really JApplet - it is the fully qualified name
javax.swing.JApplet . You can only use the unqualified name when you import the class or the
complete package containing it into your program. You can still reference a class from a package even if
you don't import it though - you just need to use the full class name, javax.swing.JApplet . You
could try this out with the applet you saw earlier if you like. Just delete the two import statements from
the file and use the full class names in the program. Then recompile it. It should work the same as
before. Thus the fully qualified name for a class is the name of the package in which it is defined,
followed by a period, followed by the name given to the class in its definition.
You could import the names of all the classes in the javax.swing package with the statement:
import javax.swing.*;
The asterisk specifies that all the class names are to be imported. Importing just the class names that
your sourcecode uses makes compilation more efficient, but when you are using a lot of classes from a
package you may find it more convenient to import all the names. This saves typing reams of import
statements for one thing. We will do this with examples of Java code in the topic to keep the number of
lines to a minimum. However, there are risks associated with importing all the names in a package.
There may be classes with names that are identical to names you have given to your own classes, which
would obviously create some confusion when you compile your code.
You will see more on how to use import statements in Chapter 5, as well as more
about how packages are created and used, and you will be exploring the use of classes
from the standard packages in considerable depth throughout the topic.
As we indicated earlier, the standard classes do not appear as files or directories on your hard disk.
They are packaged up in a single compressed file, rt.jar , that is stored in the jre/lib directory.
This directory is created when you install the JDK on your computer. A .jar file is a J ava ar chive - a
compressed archive of Java classes. The standard classes that your executable program requires are
loaded automatically from rt.jar , so you don't have to be concerned with it directly at all.
Search WWH ::




Custom Search