Java Reference
In-Depth Information
$ export CLASSPATH="/home/joeuser"
$
Now Java knows where to look to find classes of the
com.myco.financial
and
com.myco.util
packages.
The
import
Statement
3.3.2
Once we have put our classes into packages, we have to use that package's name
when we refer to those classes—unless we use the
import
statement.
Continuing our example, if we want to declare a reference to an
Account
object, but
Account
is now part of
com.myco.financial
, then we could refer
to it with its full name, as in:
com.myco.financial.Account =
new com.myco.financial.Account(user, number);
which admittedly is a lot more cumbersome than just:
Account = new Account(user, number);
To avoid the unnecessarily long names, Java has
import
statements. They
are put at the beginning of the class file, outside the class definition, just after
any
package
statement. In an
import
statement, you can name a class with
its full name, to avoid having to use the full name all the time. So our example
becomes:
import com.myco.financial.Account;
// ...
Account = new Account(user, number);
If you have several classes from that package that you want to reference,
you can name them all with a “
*
”, and you can have multiple different
import
statements, as in:
import java.util.*;
import com.myco.financial.*;
// ...
Account = new Account(user, number);
Here are a few things to remember about
import
statements. First, they
don't bring in any new code into the class. While their syntax and placement