Java Reference
In-Depth Information
Below is a list of some more rules about import declarations.
•
Duplicate single-type import and import-on-demand declarations are ignored. The following
code is valid:
// D.java
package p4;
import p1.A;
import p1.A; // Ignored. A duplicate import declaration.
import p2.*;
import p2.*; // Ignored. A duplicate import declaration.
class D {
// Code goes here
}
•
It is legal, though not needed, to import classes from the same package using single-
type import declarations or import-on-demand declaration. The following code imports
class
F
from the same package
p5
. Note that all classes declared in the same package are
automatically imported by Java. In such a case, the import declaration is ignored.
// E.java
package p5;
import p5.F; // Will be ignored
class E {
// Code goes here
}
// F.java
package p5;
import p5.*; // Will be ignored
class F {
// Code goes here
}
Automatic Import Declarations
You have been using the
String
class and the
System
class by their simple names and you never cared to import them
in any of your programs. The fully qualified names of these classes are
java.lang.String
and
java.lang.System
. Java
always imports all types declared in the
java.lang
package automatically. Think of the following import-on-demand
declaration being added to your source code before compilation:
import java.lang.*;
This is the reason that you were able to use the simple names of
String
and
System
classes in your code without
importing them. You can use any types from the
java.lang
package by their simple names in your programs.