Importing classes or interfaces from another package.
As mentioned in the introduction, the full name of a class or interface (more accurately, the fully-qualified name) consists of the package name—which may consist of multiple parts if the class is in a subpackage of another package—followed by the simple name of the class or interface. Thus, in the example given in the introduction, a class named Generator
, located in the edu.cnm.deepdive.codebreaker
package, has the fully-qualified name edu.cnm.deepdive.codebreaker.Generator
.
This fully-qualified name is actually how a class or interface is known to the Java compiler and the Java class loader. Of course, it would be very cumbersome if we had to use this fully-qualified name whenever we referred to a class in our source code. Fortunately, the Java language includes an import
statement, which can be used at the start of a source code file, to instruct the compiler to recognize a class or interface by the use of its simple name in that source file, rather than requiring the fully-qualified name.
When a Java source file includes one or more import
statements, they must follow the package
statement (if present), and precede any class or interface declarations in the file.
import {packagename[.subpackagename[...]].ClassOrInterfaceName};
(Note that the braces and brackets are part of the placeholders, and must not appear in the actual import
statement.)
packagename
The root-level package name.
subpackagename
Zero or more nested levels of subpackage names.
ClassOrInterfaceName
Simple name of the class or interface being imported.
Note that the import
statement must be terminated with a semicolon. Also, note that the braces and brackets are part of the placeholders, and must not appear in the actual import
statement. Finally, remember that classes and interfaces in the default package are effectively in a package with no name; because of this, they can’t be referenced in an import
statement of any kind! Worse, if the file being compiled is not in the default package itself, classes and interfaces in the default package will not be imported implicitly; thus, classes and interfaces in the default package cannot be used by code in any other package.
import edu.cnm.deepdive.codebreaker.Generator;
import java.util.List;
These statements instruct the compiler to treat any references (in the current source code file) to the simple names Generator
or List
as references to the class with the fully-qualified name edu.cnm.deepdive.codebreaker.Generator
or the interface with the fully-qualified name java.util.List
(respectively).
import {packagename[.subpackagename[...]]}.*;
(Once again, the braces and brackets are part of the placeholders, and must not appear in the actual import
statement.)
packagename
The root-level package name.
subpackagename
Zero or more nested levels of subpackage names.
Again, the import
statement must be terminated with a semicolon.
import java.util.*;
This statement instructs the compiler to scan the contents of the java.util
package, and to treat all classes and interfaces located in that package (but not its subpackages) as if they had been imported explicitly. Thus, references to List
, Set
, or Random
(for example)—all of which are classes or interfaces in the java.util
package—will be treated as references to java.util.List
, java.util.Set
, or java.util.Random
(respectively).
These wildcard imports are not permitted in this bootcamp, as they make reference conflicts much more likely, and can make compilation problems more confusing and harder to fix.
During compilation, the Java compiler automatically imports all classes and interfaces in the java.lang
package (but not its subpackages). In other words, the compiler behaves as if the following statement were included in every source file:
import java.lang.*;
In addition, any classes and interfaces in the same package as the file being compiled are automatically imported by the compiler. (If the file being compiled doesn’t have a package
statement, and is thus in the default package, other classes and interfaces in the default package will be imported automatically.)
The JShell REPL takes this several steps further, automatically executing the following on startup:
import java.lang.*;
import java.io.*;
import java.math.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.prefs.*;
import java.util.regex.*;
import java.util.stream.*;
These wildcard imports are coherent with the intended use of JShell—namely, as a “quick and dirty” interactive scratchpad. Thus, while they’re not permitted in the source code files we write, they—and other wildcard imports—are allowed when using JShell.