Java Reference
In-Depth Information
Every class in Java is contained in a package, including all those we have defined in our examples. You
haven't seen many references to package names so far because we have been implicitly using the default
package to hold our classes, and this doesn't have a name.
All of the standard classes in Java are contained within packages. The package that contains most of the
standard classes that we have used so far is called java.lang . You haven't seen any explicit reference
to this in your code either, because this package is automatically available to your programs. Things are
arranged this way because some of the classes in java.lang , such as String , are used in every
program. There are other packages containing standard classes that you will need to include explicitly
when you use them, as we did in the previous chapter with the StringTokenizer class.
Packaging Up Your Classes
Putting one of your classes in a package is very simple. You just add a package statement as the first
statement in the source file containing the class definition. Note that it must always be the first statement.
Only comments or blank lines are allowed to precede the package statement. A package statement consists of
the keyword, package , followed by the package name, and is terminated by a semi-colon. If you want the
classes in a package to be accessible outside the package, you must declare the class using the keyword
public in the first line of your class definition. Class definitions that aren't preceded by the keyword
public are only accessible from methods in classes that belong to the same package.
For example, to include the class Sphere in a package called Geometry , the contents of the file
Sphere.java would need to be:
package Geometry;
public class Sphere {
// Details of the class definition
}
Each class that you want to include in the package Geometry must contain the same package statement
at the beginning, and you should save all the files for the classes in the package in a directory with the
same name as the package, that is, Geometry . Note the use of the public keyword in the definition of
the Sphere class. This is to make the class usable generally.
Note that you would also need to declare the constructors and methods in the class as public if you
want them to be accessible from outside of the package. We will return to this in more detail a little later
in this chapter.
Packages and the Directory Structure
Packages are actually a little more complicated than they appear at first sight, because a package is intimately
related to the directory structure in which it is stored. You already know that the definition of a class with the
name ClassName must be stored in a file with the name ClassName.java , and that all the files for classes
within a package, PackageName , must be included in a directory with the name PackageName . You can
compile the source for a class within a package and have the .class file that is generated stored in a
different directory, but the directory name must still be the same as the package name.
Search WWH ::




Custom Search