Packages
When you start writing industrial-sized software programs, you will start using hundreds – perhaps thousands – of classes. If all these classes were stored in the same directory, it would be very difficult to keep straight what anything did. A Java package allows us to store related classes and interfaces in a separate directory.
The biggest program we will write in this class will have about 10 classes, which is not big enough to need packages. However, we will separate our files into packages anyway, so as to get practice using them.
Putting Code in a Package
When you decide to place a class or interface in a package, first place that class or interface in a directory with the package name. Next, add the line:
package packageName;to the top of the file, where packageName is the name of the package. (Note: most
development environments handle packages for you. If you are using something like Eclipse,
find an “Add Package” option and supply the name of the package. Next, try to add a class or
interface to that package. The development environment will handle all the details for you.)
Using a Package
If you want to use code from a package called packageName, add:
import packageName.*;to the file where you want to use the code. This is exactly like importing Java packages. Now, you will be able to use all the classes and interfaces in that package.