import Statement
An import
statement tells the Java compiler how to resolve external classes used by a program. Here’s an example that references a particular class (named JOptionPane
) defined in the package javax.swing
:
import javax.swing.JOptionPane;
Here are some helpful rules for working with import
statements:
import
statements must appear at the beginning of the class file, before any class declarations.
You can include as many import
statements as necessary to import all the classes used by your program.
You can import all the classes in a particular package by listing the package name followed by an asterisk wildcard, like this:
import javax.swing.*;
Because many programs use the classes that are contained in the java.lang
package, you don’t have to import that package. Instead, those classes are automatically available to all programs. The System
class is defined in the java.lang
package. As a result, you don’t have to provide an import
statement to use this class.