Java Reference
In-Depth Information
allows the source code to refer to
Applet
rather than
java.applet.Applet
.
That is, instead of
public class MyApplet extends java.applet.Applet {...}
we use
import java.applet.Applet;
public class MyApplet extends Applet {...}
Any appearance of the name
Applet
is automatically interpreted as
java.
applet.Applet
. There can be multiple
import
statements to import fully
qualified class names from multiple packages. All
import
statements appear at
the top of a class definition file, immediately after the
package
statement and
before the opening line of the class body itself. Of course, valid comments may
appear anywhere.
The
java.applet
package is small, containing only the
Applet
class. A
large package like
java.io
contains (in Java 1.4.2) 50 classes plus 16 exception
classes and 10 interfaces (see Chapter 9 for a description of I/O in Java). To use the
import
notation for each of these classes could become unwieldy even though
a typical program would be highly unlikely to use more than just a few of the
50 classes. For example, a typical program might use the following classes from
the
java.io
package:
BufferedWriter
,
FileInputStream
,
FileOut-
putStream
,
FileReader
,
FileWriter
, etc. That program would also likely
check for at least
java.io.IOException
if not some of the sub-exceptions
like
java.io.EOFException
and
java.io.FileNotFoundException
.
In this case the list of imports at the top of the file would look like this:
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutoutStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.EOFException;
import java.io.FileNotFoundException;
...
public class MyClass {...}
Using classes from several other packages, as most non-trivial programs do,
would lead to a very long list of
import
statements. To reduce the size of the
list, the compiler also supports the
*
notation to import an entire package at once
rather than specifying each imported class name separately. Therefore, the above
example can be reduced to
import java.io.*;
...
public class MyClass
{
...
}
Search WWH ::
Custom Search