13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

84Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to Java$ export CLASSPATH="/home/joeuser"$Now Java knows where to look to find classes of thecom.myco.financial and com.myco.util packages.3.3.2 The import StatementOnce we have put our classes into packages, we have to use that package’s namewhen we refer to those classes—unless we use the import statement.C<strong>on</strong>tinuing our example, if we want to declare a reference to an Accountobject, but Account is now part of com.myco.financial, then we could referto 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 l<strong>on</strong>g names, Java has import statements. Theyare put at the beginning of the class file, outside the class definiti<strong>on</strong>, just afterany package statement. In an import statement, you can name a class withits full name, to avoid having to use the full name all the time. So our examplebecomes: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 importstatements, 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, theyd<strong>on</strong>’t bring in any new code into the class. While their syntax and placement

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!