Package = directory. Java classes can be grouped together in packages. A package name is the same as the directory (folder) name which contains the .java files. You declare packages when you define your Java program, and you name the packages you want to use from other libraries in an import statement.
The first statement, other than comments, in a Java source file, must be the package declaration.
Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.
Default package. Altho all Java classes are in a directory, it's possible to omit the package declaration. For small programs it's common to omit it, in which case Java creates what it calls a default package. Sun recommends that you do not use default packages.
The statement order is as follows. Comments can go anywhere.
// This source file must be Drawing.java in the illustration directory.
package illustration;
import java.awt.*;
public class Drawing {
. . .
}
The JOptionPane class is in the swing package, which is located in the javax package. The wildcard character (*) is used to specify that all classes with that package are available to your program. This is the most common programming style.
import javax.swing.*; // Make all classes visible altho only one is used.
class ImportTest {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
Classes can be specified explicitly on import instead of using the wildcard character.
import javax.swing.JOptionPane; // Make a single class visible.
class ImportTest {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
Alternately we can the fully qualified class name without an import.
class ImportTest {
public static void main(String[] args) {
javax.swing.JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
There are 166 packages containing 3279 classes and interfaces in Java 5. However, only a few packages are used in most programming. GUI programs typically use at least the first three imports.
import java.awt.*; | Common GUI elements. |
import java.awt.event.*; | The most common GUI event listeners. |
import javax.swing.*; | More common GUI elements. Note "javax". |
import java.util.*; | Data structures (Collections), time, Scanner, etc classes. |
import java.io.*; | Input-output classes. |
import java.text.*; | Some formatting classes. |
import java.util.regex.*; | Regular expression classes. |
A: No, import only tells the compiler where to look for symbols.
A: No. The search for names is very efficient so there is no effective difference.
A: This shows good intentions, but ...
A: The wildcard "*" only makes the classes in this package visible, not any of the subpackages.
A: All classes in the java.lang package are visible without an import.
A: No. Group them for readability.
If you forgot to write import statements, or don't remember
which package a class is in, no problem. Just right click on the source file
and choose Fix Imports. It will add all necessary import
statements.
Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name. For example, after
import static java.awt.Color;
It would then be possible to write
Color background = RED;
instead of
Color background = Color.RED;
Adding this "feature" wasn't the best idea because it leads to name pollution and confusion about which class constants come from. Even Sun (see References below) basically advises not to use it!