Help: Working With Packages


Help is available for each task, or you can go straight to the solution source code.

Task 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.
Create a new file Employee.class, move the complete class declaration to this file. Add the public keyword to the class declaration. At the beginning of the file, add a package statement with CompanyPkg as the name of the package.

Task 2

Do the same with the Company class.
See help for the first task.

Task 3

Add the necessary import statement to the PackageTest.java file.
Add an import statement at the start of PackageTest.java, it will import CompanyPkg.

Task 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.
Problems here generally have to do with the Java class loader not being able to find the classes in the new package. Check the CLASSPATH variable. Note that the .java files for Company and Employee do not have to be in the CompanyPkg directory.

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