The Java class libraries are divided into packages of related classes and interfaces. Most Java programs you build reuse existing packages, such as the Java class libraries, third party libraries or libraries developed within your organization. This tutorial shows how create your own packages of classes for reuse or distribution. The tutorial is intended for students or professionals who are alreadyfamiliar with building Java classes.
Download the code for this tutorial here.
[Note: This tutorial is an excerpt (Section 8.16) of Chapter 8, Class and Objects: A Deeper Look, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.390-396. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
8.16 Time Class Case Study: Creating Packages (Continued)
Step 4: Importing the Reusable Class
Once the class is compiled and stored in its package, the class can be imported into programs (Step 4). In the Time1PackageTest application of Fig. 8.19, line 3 specifies that class Time1 should be imported for use in class Time1PackageTest. Class Time1PackageTest is in the default package because the class’s .java file does not contain a package declaration. Since the two classes are in different packages, the import at line 3 is required so that class Time1PackageTest can use class Time1.
|
||
| Fig. 8.19 | Time1 object used in an application. | |
|

