Java Reference
In-Depth Information
class Test { util.Random generator; }
// incorrect: compile-time error
Example 7.5.1-4. Importing a Type Name that is also a Package Name
Package names and type names are usually different under the naming conventions
unconventionally-named package
Vector
, which declares a public class whose name is
Mosquito
:
package Vector;
public class Mosquito { int capacity; }
and then the compilation unit:
package strange;
import java.util.Vector;
import Vector.Mosquito;
class Test {
public static void main(String[] args) {
System.out.println(new Vector().getClass());
System.out.println(new Mosquito().getClass());
}
}
the single-type-import declaration importing class
Vector
from package
java.util
does
not prevent the package name
Vector
from appearing and being correctly recognized in
subsequent
import
declarations. The example compiles and produces the output:
class java.util.Vector
class Vector.Mosquito
7.5.2. Type-Import-on-Demand Declarations
A
type-import-on-demand declaration
allows all accessible types of a named package or
type to be imported as needed.
TypeImportOnDemandDeclaration:
import
PackageOrTypeName
. * ;
The
PackageOrTypeName
must be the canonical name (§
6.7
) of a package, a class type, an
interface type, an enum type, or an annotation type.