Java Reference
In-Depth Information
Once the package is created, you can use the appropriate import command in your
program to make use of the class . For example, to use the class Clock , as created in
the preceding code, you use the following import statement in your program:
import jpfpatpd.Appendix.clockPackage.Clock;
In Java, package is a reserved word.
Example D-1 further explains how to use a package in a program. We assume that
the
has
been
compiled
and
placed
in
the
subdirectory
class
Clock
c:\jpfpatpd\Appendix\clockPackage .
EXAMPLE D-1
The following program uses the class Clock .
import jpfpatpd.Appendix.clockPackage.Clock;
public class TestClock
{
public static void main(String[] args)
{
Clock myClock = new Clock(12,30,45);
System.out.println("myClock: " + myClock);
}
}
Because this program uses the class Clock , when you compile the program using the
compiler command, you use the option -classpath to specify where to find the
compiled code of the class Clock . Suppose that the file TestClock.java is in the
subdirectory c:\jpfpatpd . Consider the following command:
javac -classpath c:\ TestClock.java
This command finds Clock.class in the subdirectory
c:\jpfpatpd\Appendix\clockPackage . The compiled code TestClock.class of
the program TestClock.java is placed in the current subdirectory. On the other hand,
the following command places the compiled code, TestClock.class , in the subdir-
ectory c:\jpfpatpd :
javac -d c:\jpfpatpd -classpath c:\ TestClock.java
Suppose the file TestClock.class is in the subdirectory c:\jpfpatpd . The following
command executes the program TestClock.class :
java -classpath .;c:\ TestClock
If you are using an IDE to create Java programs, you need to be familiar with the
commands to compile and execute them. Typically, an IDE automatically stores the
compiled code of the classes in an appropriate subdirectory.
 
Search WWH ::




Custom Search