Java Reference
In-Depth Information
Yo u can also compile the package files directly, but if the compilation occurs
from the higher directory, you must specify the proper directory name:
C:
\
myApps> javac mypack/TestAB.java
(The
javac
compiler interprets the forward slash
“/
„
as a directory separator.
The backslash
“
\
„
also works on Windows.) Similarly, the JVM by default looks
for class files in subdirectories as specified by the package names.
Just as a subdirectory can itself contain lower levels of subdirectories,
packages can be nested to lower levels as well. For example, the class def-
inition for
TestC
shown below includes the directive
“package mypack.
extrapack;
„
. The
TestC.java
file must then go into the subdirectory
myApps/mypack/extrapack
(or
myApps
\
mypack
\
extrapack
onaMS
Windows platform) as shown in Figure 5.1(b).
package mypack.extrapack;
public class
TestC
{
public boolean flag;
public TestC (boolean b) {
flag = b;
}
}
When the compiler acts on the
TestABC
class shown below, it uses the
class specification
mypack.extrapack.TestC
to look in subdirectory
mypack/extrapack/
relative to the directory of
TestABC
.Itcompiles
TestC.java
if there is no class file or if the class file is older than the
TestC.java
file.
public class
TestABC
{
public static void main (String[] args) {
mypack.TestA testa = new mypack.TestA (4);
mypack.TestB testb = new mypack.TestB (1.3);
mypack.extrapack.TestC testc =
new mypack.extrapack.TestC (true);
if (testc.flag)
System.out.println (
"
testa.a
="
+ testa.a);
else
System.out.println (
"
testb.x
="
+ testb.x);
}
}
Search WWH ::
Custom Search