Java Reference
In-Depth Information
other. If a source file resides within a different package than another file that it needs to
use, an
import
statement must be declared at the top of the source file (underneath
the package statement) to import that other file for use; otherwise, the fully qualified
package.class
name must be used within the code. Classes may be imported sep-
arately, as demonstrated in the following
import
statement:
import org.juneau.JuneauWidgets;
However, it is often likely that all classes and type files that reside within a package
need to be used. A single
import
statement utilizing a wildcard character (
*
) can im-
port all files within a named package as follows:
import org.juneau.*;
Although it is possible to import all files, it is not recommended unless absolutely
necessary. As a matter of fact, it is considered a poor programming practice to include
many
import
statements that use the wildcard. Instead, classes and type files should
be imported individually.
Organizing classes within packages can prove to be very helpful. Suppose that the
widget application that was described in the solution to this recipe includes different
Java classes for each different widget object. Each of the widget classes could be
grouped into a single package named
org.juneau.widgets
. Similarly, each of the
widgets could extend some Java type or interface. All such interfaces could be organ-
ized into a package named
org.juneau.interfaces
.
Any substantial Java application will include packages. Any Java library or Applic-
ation Programming Interface (API) that you use includes packages. When you import
classes or types from those libraries and APIs, you are really importing packages.
1-10. Configuring the CLASSPATH
Problem
You want to execute a Java program, or include an external Java library in the applica-
tion you are executing.
Solution