One scheme in common use is to use your domain name, with its elements
reversed, as the prefix for all your package names. For example, the Apache Project
produces a networking library as part of the Apache Commons project. The Com‐
mons project can be found at http://commons.apache.org/ and accordingly, the pack‐
age name used for the networking library is org.apache.commons.net .
Note that these package-naming rules apply primarily to API developers. If other
programmers will be using classes that you develop along with unknown other
classes, it is important that your package name be globally unique. On the other
hand, if you are developing a Java application and will not be releasing any of the
classes for reuse by others, you know the complete set of classes that your
application will be deployed with and do not have to worry about unforeseen nam‐
ing conflicts. In this case, you can choose a package naming scheme for your own
convenience rather than for global uniqueness. One common approach is to use the
application name as the main package name (it may have subpackages beneath it).
When referring to a class or interface in your Java code, you must, by default, use
the fully qualified name of the type, including the package name. If you're writing
code to manipulate a file and need to use the File class of the java.io package, you
must type java.io.File . This rule has three exceptions:
• Types from the package java.lang are so important and so commonly used
that they can always be referred to by their simple names.
• The code in a type p.T may refer to other types defined in the package p by
their simple names.
• Types that have been imported into the namespace with an import declaration
may be referred to by their simple names.
The first two exceptions are known as “automatic imports.” The types from
java.lang and the current package are “imported” into the namespace so that they
can be used without their package name. Typing the package name of commonly
used types that are not in java.lang or the current package quickly becomes tedi‐
ous, and so it is also possible to explicitly import types from other packages into the
namespace. This is done with the import declaration.
import declarations must appear at the start of a Java file, immediately after the
package declaration, if there is one, and before any type definitions. You may use
any number of import declarations in a file. An import declaration applies to all
type definitions in the file (but not to any import declarations that follow it).
The import declaration has two forms. To import a single type into the namespace,
follow the import keyword with the name of the type and a semicolon:
import java.io.File ; // Now we can type File instead of java.io.File
This is known as the “single type import” declaration.