import declarations


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 ArrayList and LinkedList. If you need to use these, you can write declarations such as

     java.util.ArrayList myArrayList = new java.util.ArrayList();
     java.util.LinkedList myLinkedList = new java.util.LinkedList();

or you can import these classes

     import java.util.ArrayList;
     import java.util.LinkedList;

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:

     import java.util.*;

One important package, java.lang, is automatically imported for you.

If you use a package declaration, it must be the first thing in your Java file, and the import declarations must be put immediately after it. If you do not have a package declaration, the 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.