Java is an object oriented programming language and any Java programmer will quickly build a large number of different classes for use in each application that they develop.
Initially it may be tempting to store the classes in the same directory as the end application, but as the complexity applications grow so will the number of classes. The answer is to organize the classes into packages.
The Java Package
A Java package is simply a directory that contains one or more class files. It is, therefore, just a matter of:
- creating a folder
- moving the class file to the new folder (or creating them there)
- calling the package from any applications
Of course, nothing is ever quite that simple.
Creating a Java Package
The first step is (rather obviously) to create the directory for the package. This can go in one of two places:
- in the same folder as the application that will be using it
- in a location listed in the system CLASSPATH environmental variable
So, for example, if CLASSPATH contains C:\Java\Packages then the new package can be created there, for example C:\Java\Packages\suite101, and once the directory has been created then classes can be added to it.
The first line of a class in a package is essential - it must contain the name of of the package (i.e. the directory in which the class resides):
No code is allowed above the package statement, but after that critical line the class can be defined as normal. For example it can call any packages that it requires:
In this example the class contains a single method which returns today's date formatted for the current locale:
As always the code must be saved to a text file with the same name as the class - in this case that's C:\Java\Packages\suite101\formatted_date.java.
Using a Java Package
Packages are loaded into new applications by using the import method as shown in the above example where the following line was used:
This is a statement recognized by many Java programmers and means "load all of the the classes stored in the the java.util package". Custom packages are loaded into a Java application exactly the same way:
This will load any classes found in the suite101 directory (the location of which is defined by the CLASSPATH environmental variable). The formatted_date class can now be used in the class for the new application:
The application code must be saved to a file with the same name as its class, but this file may be placed in any location on the computer (for example C:\Java\Apps\creating_packages\use_packages.java) and this can be compiled and run as required.
Summary
Java packages are used to group and organize classes:
- a packages is a folder (or directory) on a computer
- the location of packages are defined by using the CLASSPATH environmental variable
- the package may contain one or more classes
- each class in the package must have the following as its first line:package <package name>;
- The classes in the package are loaded into a Java application by using:import <package name>.* ;
Giving the Java programmer the ability to create and to organize classes in the most efficient ways possible.