Packages and Import

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.

Package declaration

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.

Imports: three options

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.

It's hard to remember to remove classes when they are no longer used,
so the import list is surprisingly often wrong. It can seriously slow
down reading because unusual or unexpected class imports make me look for that
class, only to discover that it must have been used in an earlier version.

Explicit class imports permit accidentally defining classes with names
that conflict with the standard library names. This is very bad.
Using "*" to import all classes prevents this dangerous naming accident.

It's annoying to always update this list, altho if you use NetBeans,
fixing the list is only a click away (see below).

Q: I've imported java.awt.*, why do I also need java.awt.event.*?

A: The wildcard "*" only makes the classes in this package visible,
not any of the subpackages.

Q: Why don't I need an import to use String, System, etc?

A: All classes in the java.lang package are visible without an import.

Q: Is the order of the imports important?

A: No. Group them for readability.

NetBeans creates packages by default

The project name is used as the default package name, but you can change it.

A directory / folder is created with this project name. This directory
name is the name of your package.

A package declaration is automatically inserted into each new source file it creates.

When you build a main project, the double-clickable .jar file
uses this project/package/directory name.

NetBeans will create your imports

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.

Static imports in Java 5

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!