Java Reference
In-Depth Information
class, then be sure the class path includes the current directory, which is indicated
by a dot.
When you run the class compiled as just shown, you should again use the
-classpath
option as indicated below:
java -classpath .;C:\libraries\nemeric;C:\otherstuff YourClass
It is important to include the current directory on the class path when you run the
program. If your program is in the default package, it will not be found unless you
include the current directory. It is best to get in the habit of always including the cur-
rent directory in all class paths.
Because the class path specified in compiling and running your classes is input to a
program (
javac
or
java
) that is part of the Java environment and is not a command to
the operating system, you can use either
/
or
\
in the class path, no matter which of
these two your operating system uses.
Name Clashes
★
In addition to being a way of organizing libraries, packages also provide a way to deal
with
name clashes.
A
name clash
is a situation in which two classes have the same
name. If different programmers writing different packages have used the same name
for a class, the ambiguity can be resolved by using the package name.
Suppose a package named
sallyspack
contains a class called
HighClass
, and
another package named
joespack
also contains a class named
HighClass
. You can use
both classes named
HighClass
in the same program by using the more complete
names
sallyspack.HighClass
and
joespack.HighClass
. For example:
name clash
sallyspack.HighClass object1 =
new
sallyspack.HighClass();
joespack.HighClass object2 =
new
joespack.HighClass();
These names that include the package name, such as
sallyspack.HighClass
and
joespack.HighClass
, are called
fully qualified class names
.
If you use fully qualified class names, you do not need to import the class, since this
longer class name includes the package name.
fully
qualified
class name
Self-Test Exercises
44. Suppose you want to use the class
CoolClass
in the package
mypackages.library1
in a program you write. What do you need to do to make this class available to
your program? What do you need to do to make all the classes in the package
available to your program?
45. What do you need to do to make a class a member of the package named
mypackages.library1
?
46. Can a package have any name you want, or are there restrictions on what you
can use for a package name? Explain any restrictions.