Table of ContentsExceptions

Packages, Import, and CLASSPATH

Packages let you organize related classes into hierarchical collections. You can think of packages in terms of modules or libraries. Most of the functionality available in the Java API, such as java.lang and java.util,comes in the form of a package. You can create your own packages to better organize and segment functionality into modules.

To use a class from within a package, you actually don't need to do anything. As long as the class is visible to your application (via the CLASSPATH), you can reference it within your code. To do so, just add the package name to the class name. For example:

java.lang.String s = new java.lang.String();

Of course, having to add the full package name all over your code can get a little cumbersome. Wouldn't it be nice if the compiler could just assume which package you meant? I mean, how many String classes are there? Then you could just write:

String s = new String();

The compiler has a problem, though. The potential number of packages it has to scan can quite easily run into the thousands. (Just the Java API is hundreds of classes.) This would seriously slow down compiling, and it would have to happen on every compile. To solve this problem, you use the import keyword to limit the scope of the classes you intend to use. The same problem still holds true: The more you import, the longer the compiler will take to scan the import list. However, with a limited import scope, the length of time is usually negligible. Just don't go too ballistic on your import statements. Here's the example, rewritten with an import statement:

import java.lang.String;

class x
{
    String s = new String();
}

This works great, but you can imagine that with a big class, you might use hundreds of different classes (well, tens anyway). The import list would take up three screens! To avoid this, you can import using a wildcard. For example:

import java.lang.*;

class x
{
    String s = new String();
}

You can now use any class in the java.lang package without naming it specifically.

NOTE

Tip

Try to avoid using wildcard imports when you can. Some packages are very large, and this can quickly slow down your compiles if you're not careful. Use a specific class import by default, and if the number of classes you're importing for a single package exceeds five, switch to the wildcard import. Some IDEs, such as IDEA, will automatically do this for you.

Packages are a great way to organize your work into modules. You can create your own packages using the package keyword. For example:

package my.utils;
import java.lang.*;

class x
{
    String s = new String();
}

However, for this to work you need to move the source for this class into a directory that matches the package namein this case, a subdirectory called my under another directory called utils.So the final class name must appear as my/utils/x.class.This directory must be visible to your compile and run-time CLASSPATH.

CLASSPATH is very similar to any other type of system pathit declares the root paths to search for classes. You usually set the CLASSPATH using your IDE or from the command line. For example:

set CLASSPATH = c:\java\lib;.;

NOTE

Tip

In Windows, CLASSPATH elements use a semicolon (;). In UNIX, they use a colon (:).

To make your classes available, you need to ensure that the class files are visible to the CLASSPATH.

    Table of ContentsExceptions