We have mentioned in the introduction to this course that Java's
design allows for relatively easy sharing of code.
For example, one often doesn't need to recompile subclasses
even if they extend third-party classes that have been altered
by their authors.
For scientific work, distributing code is important for taking
advantage of other programs that provide capabilities such as
graphing, math functions, numerical analysis tools, etc.
Packages are the usual way
of distributing such code. A package, unless it comes in a JAR
file, must be put into a subdirectory heirarchy that matches
it's package name.
Also, the subdirectory, or JAR file, must be added to the CLASSPATH
if it is not currently accessible.
For example, in the Chapter
5: Tech section we discuss formatting of numerical strings
and refer to two freeware classes - Format
and Console
- from the book Core Java by C. S. Horstmann
& G. Cornell.
Since the source code for these classes is available, we can
put the compiler directive
package
corejava.*;
at the top of each file and put the files into a corejava
directory.
For javac
and java
to find the files, the directory corejava must either be a subdirectory
where our classes are located or the CLASSPATH
must point to its location.
Note:
by convention, package names are lowercase.
Latest Update: Oct. 22, 2004