Java Reference
In-Depth Information
Figure 5.1 (a) Directory hierarchy for the package mypack . (b) Directory hierarchy for
the package myPack and its sub-package mypack.extrapack.
the class type name. For example, we illustrate how to use package mypack with
the program TestAB.java , shown below.
public class TestAB {
public static void main (String[] args) {
mypack.TestA testa = new mypack.TestA (4);
mypack.TestB testb = new mypack.TestB (31.3);
System.out.println ("Prod ="+ (testa.a * testb.x));
}
}
The package names are relative to the current directory or to a classpath
setting. (We discuss the CLASSPATH setting in Section 5.7.) If the program
TestAB.class resides in the c: \ myApps directory, then by default the
compiler and JVM look for the packages relative to this directory. The term
mypack.TestA tells the compiler to look for the TestA.class in the sub-
directory mypack relative to the directory where TestAB.class resides.
Figure 5.1(a) shows the directory hierarchy for the files.
Move to the directory myApps and compile the file:
C: \ myApps> javac TestAB.java
The compiler looks for the TestA.class and TestB.class files in the
mypack subdirectory relative to the current directory. They are compiled if
the compiler does not find class files newer than the last modification date of
the source files. The compiler sees the combination of the package and class
names for mypack.TestA and mypack.TestB and treats the “. as if it were
the directory name separator.
Search WWH ::




Custom Search