Working With Packages


Prerequisites

Packages in Java are used to group related classes (as well as interfaces and exceptions). They help to organize large numbers of classes into functional groups (such as with the JDK), to control the scoping of names, and allow for hiding of utility classes.

In this Magercise, the solution to the previous one will be broken up into a new package, CompanyPkg, which contains the Company and Employee classes, and the PackageTest file, which imports this package and uses it.

Packages are discussed in detail in the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.packages.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Move the Employee class into a new file. Add a package statement to this file. Make the class public so it can be seen outside the package.

  2. Do the same with the Company class.

  3. Add the necessary import statement to the PackageTest.java file.

  4. Compile the files and run the program. The .class files for the classes in the new package should be in a directory whose name is CompanyPkg. This directory should be in the same directory as the PackageTest.java file that you are compiling. Or, it may be located elsewhere, provided that the environment variable CLASSPATH includes a directory that contains the CompanyPkg directory.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.