Import declarations, sometimes called import "statements,"
make it possible to abbreviate references to data and methods in other packages.
For example, the package java.util contains many useful classes, such as
LinkedList. If you need to use these, you can write declarations
java.util.ArrayList myArrayList = new java.util.ArrayList();
java.util.LinkedList myLinkedList = new java.util.LinkedList();
or you can import these classes
and simplify your declarations to:
ArrayList myArrayList = new ArrayList();
LinkedList myLinkedList = new LinkedList();
In addition, you can use an asterisk (
*) as a "wild card"
and import all the classes in a package at once:
One important package,
java.lang, is automatically imported for
If you use a
package declaration, it
must be the first thing in your Java file, and the
must be put immediately after it. If you do not have a
import declarations must be the first thing in
your Java file.
If you use only a few classes from a package, it is good style to import them individually. This makes it easier to see which classes your program actually uses.
If you use a lot of classes from a package, it probably makes more sense to use a wild card to import them all.