Page 790 - Introduction to Programming with Java: A Problem Solving Approach
P. 790

                756 Appendix 4 Packages
with the javax package at its root. This picture includes all of the API packages we import at some point in this book, but the packages shown in Figure A4.1 are only a small fraction of all the packages in the API package hierarchies.
This hierarchical organization helps people locate particular classes they need to use. It’s OK for sev- eral different classes to have the same class name if they are all in different packages. So encapsulating small groups of classes into individual packages allows a given class name to be reused in different contexts. Also, each package protects the protected members of the classes it includes from access from outside that package.
To make the classes in a particular package available to a program you are writing, you import that package, as in these statements, which import the java.util and java.awt packages:
import java.util.*;
import java.awt.*;
Figure A4.1 includes some packages at a third level down from the top. Consider, for example, the java. awt.event package under java.awt in the java tree. When we import the java.awt package in the statement above, this provides access to all classes in the java.awt package itself, but it does not also pro- vide access to packages under it. In other words it does not also import the java.awt.event package. If you also need access to classes in the java.awt.event package, you also must import that package explicitly by adding this third import statement:
import java.awt.event.*;
Custom Packages
Apago PDF Enhancer
The Java language permits you to create your own packages for organizing programmer-defined classes into package hierarchies. This involves the following steps:
First, design a package structure that makes sense for the program you are creating. Then create a di- rectory structure that corresponds exactly to that package structure. (Later, we’ll show how to create this directory structure automatically, but the manual process we’re describing now is easier to understand.) Figure A4.2a shows part of a package structure that could be used for this book’s examples. Figure A4.2b shows the corresponding directory structure. Note “IPWJ” at the top of both figures. IPWJ is an acronym for our book’s title, Introduction to Programming with Java.
Whenever you compile a class you want to be in a package, insert this line at the top of the class, above all statements, even the import statements:
package <package-path>
The package path is a sub-directory path, except it uses a dot (.) instead of a slash (/ or \). The first name in the package path should be the name of the root of the package hierarchy and the name of the highest di- rectory in the part of the directory structure that corresponds to it. The last name in the package path should be the name of the directory that will contain the class being defined. So, for example, if you are defining a Car class and you intend for the Car.class bytecode file to be in the IPWJ.chapter13.things package shown in Figure A4.2a, the first statement in your Car.java file should be:
package IPWJ.chapter13.things;
When you compile your Car.java source code, by default the generated Car.class bytecode goes into the current directory, and the package statement above does not by itself change that. Thus, if you choose to write your source code in the ...IPWJ/chapter13/things directory, the Car.class file goes im- mediately into this directory also. If you want this directory to include both source code and bytecode, you’re


















































































   788   789   790   791   792