Java Reference
In-Depth Information
With this notation, our class can use any of the 50 classes, 16 exceptions, and 10
interfaces in the java.io package by referring simply to the short names.
What the import statement with the * notation really does is instruct the
compiler into which package(s) to look whenever a short unqualified class name
is encountered. Therefore, in the example above, if an unqualified FileWriter
class name appears, the compiler is able to find that class in the java.io package.
It is not an error to use the fully qualified name even when an import statement
appears that would permit the short unqualified name.
Note that the * notation does not look into sub-packages. For example, import-
ing java.awt.* does not also import java.awt.event.* .
As a complete but simple example, consider our TestABCApplet class,
which uses the import directives
import mypack.*;
import mypack.extrapack.*;
These instruct the compiler to look in the mypack package and the
mypack.extrapack package for any class references that cannot be found
in the default package ( java.lang ).
import java.applet.Applet;
import mypack.*;
import mypack.extrapack.*;
public class TestABCApplet extends Applet
{
public static void main (String[] args) {
TestA testa = new TestA (4);
TestB testb = new TestB (31.3);
TestC testc = new TestC (true);
if (testc.flag)
System.out.println ("testa.i ="+ testa.i);
else
System.out.println ("testa.x ="+ testb.x);
}
// Paint message in Applet window.
public void paint (java.awt.Graphics g) {
g.drawString ( " TestABCApplet " , 20, 20);
}
}
In rare circumstances, two different packages might use the same class name. In
this case the compiler cannot know which class you really want. For example,
 
Search WWH ::




Custom Search