Java Reference
In-Depth Information
Chapter
11
Packages
OBJECTIVES
• Provide an understanding of packages in Java.
• Learn how to set variable CLASSPATH.
INTRODUCTION
People invent classifications and categories in order to make things manageable.
In that old game of twenty questions, things of the world are divided into three
categories: animal, mineral, or vegetable. Animals are further categorized into
different kinds, as are minerals and vegetables. And, books in a library are clas-
sified using the Dewey Decimal system. A hierarchy of categories goes as deep
as need be to provide structure and make things manageable.
Because there are hundreds of Java classes to deal with, Java provides a
mechanism for classifying them: the package.
11.1
Using packages
Placing a class in a package
A
package
is a collection of classes that have been grouped together and
reside in the same folder, or directory, on a computer.
Suppose we have a class
OneClass
in a file
OneClass.java
. To specify that
this class belongs to a package named
package1
, place a package statement on
the first line of file
OneClass.java
:
package
package1;
public class
OneClass {
…
}
It is your obligation to see to it that file
OneClass.java
is placed in direc-
Search WWH ::
Custom Search