6.1 Packages 

Previous Index Next 

What Are packages

Packages are used in Java to group like classes together. The Java API is made up of several different packages. The naming structure of packages is hierarchical.

For instance all the Java API packages start with the word java. Some of the Java API packages are:

Using Packages

If you want to use a class from a package you must first import the package. To do this you use the import statement. These statements must be before the class declaration.

For instance to import the Socket class for the java.net package, you would use the following statement:

import java.net.Socket;
If you wish to import all the classes in a package you can use a * instead of a class name. To import all the classes in the java.io package you would use the following statement:
import java.net.*;
The java.lang package is automatically imported by the java compiler.

Creating a Package

You can create you own packages and put you classes into them. The package statement is used to declare which package the class belongs to. The following code declares a class called MyClass in the guide.example package.
package guide.example;

public class MyClass
{
  //....class body goes here
}
Only public classes can be accesed in a package.

How the Java VM finds classes

In the Java API there is a special class called ClassLoader. This class is used by the Java VM to load the .class files for an application. By default the ClassLoader in the Java VM loads the classes from disk, but this can be changed. An example of this, is the class loader used for applets. This class loader loads the classes from a web server.

The default class loader uses an environment variable called CLASSPATH. This variable contains a list of directories that the class loader searches for class files.

To find a file the class loader takes the package name for a class and turns it into a directory path. For example when trying to load MyClass from the guide.example package it will take the package name (guide.example) and convert it into \guide\example\.

Then for each directory in the CLASSPATH it will look for a  sub-directory that matches this path (\guide\example\ in this case).

If it finds a sub-directory it then looks for the class file. In the example the file would be called MyClass.class. If it finds the class file it will load it memory.

The default class loader also has the ability to look inside archive files (.zip or .jar files) for classes. If an archive file is included in the CLASSPATH, the class loader will open this file and search through the directory structures held in the archive, to find the class it is looking for.

Setting up a directory structure to hold your own classes

Because of the way the Java VM searches for the class files the best way to set up a directory structure to hold your .java and .class files is as follows:
  1. Create a directory somewhere (eg C:\javasrc\). This is your base directory.
  2. Modify the CLASSPATH environment variable so that your base directory is included in it.
  3. Build a sub-directory structure, under your base directory, that matches your package hierarchy.
  4. Place the your .java files in the directory that matches the package that the class belongs to. The Java compiler will place the .class file in the same place and the .class will therefore be found when the class loader attempts to load it.
Sun Microsystems have also suggested that all commercial Java classes live in packages that start with com.company. For instance all Sun's non-API classes belong to packages starting with com.sun and IBM's Java classes are all in com.ibm packages. The eliminates the chances of packages being created with the same name by different vendors.