Java Reference
In-Depth Information
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 earli-
er 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 in the package are to be imported. Importing just the class
names that your source code uses makes compilation more efficient, but when you are using a lot of classes
from a package you find it more convenient to import all the names. This saves typing reams of import
statements for one thing. I use the * notation to import all the names in a package in many of the working
examples in the topic to keep the number of lines to a reasonable level. However, there is a downside asso-
ciated with importing all the names in a package.
If you import just the class names that you need from library packages, you minimize the risk of name
collisions. A name collision occurs when a class name that you have imported from a package is identical to
a name you have given to one of your own classes. This obviously creates some confusion when you com-
pile your code because the compiler is not able to deduce which class you are referencing when you use the
class name. By default, an unadorned class name is taken to be the class in your program. Using the * in an
import statement to import all the names in a package greatly increases the likelihood of name collisions.
Sometimes name collisions are unavoidable. For example, the name Action is used in two different pack-
ages for different types. When a name collision occurs, you can use the fully qualified name for either or
both of the names that collide to enable the compiler to understand what you are referencing.
NOTE You read more on how to use import statements in Chapter 5, as well as more about
how packages are created and used, and you explore the use of classes from the standard
packages in considerable depth throughout the topic.
The standard classes do not appear directly in files and directories on your hard disk. They are contained
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 Java archive — a compressed archive of Java classes.
The name rt stands for run time because rt.jar contains all the classes that comprise Java's core API. 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.
Java Applications
Every Java application contains a class that defines a method called main() . The name of the class that con-
tains main() is the name that you use as the argument to the Java interpreter when you run an application.
You can call the class whatever you want, but the method that is executed first in an application is always
called main() . When you run your Java application, execution starts with the main() method. The main()
Search WWH ::




Custom Search