Packages

If you are developing an application that consists of different parts, you may want to organize your classes to retain maintainability.

With Java, you can group related classes or classes with similar functionality in packages.

For example, standard Java classes come in packages. Java core classes are in the java.lang package. All classes for performing input and output operations are members of the java.io package, and so on.

If a package needs to be organized in more detail, you can create packages that share part of the name of the former. For example, the Java class library comes with the java.lang.annotation and java.lang.reflect packages. However, mind you that sharing part of the name does not make two packages related. The java.lang package and the java.lang.reflect package are different packages.

Package names that start with java are reserved for the core libraries. Consequently, you cannot create a package that starts with the word java. You can compile classes that belong to such a package, but you cannot run them.

In addition, packages starting with javax are meant for extension libraries that accompany the core libraries. You should not create packages that start with javax either.

In addition to class organization, packaging can avoid naming conflict. For example, an application may use the MathUtil class from company A and an identically named class from another company if both classes belong to different packages. For this purpose, by convention your package names should be based on your domain name in reverse. Therefore, Sun’s package names start with com.sun. My domain name is brainysoftware.com, so it’s appropriate for me to start my package name with com.brainysoftware. For example, I would place all my applets in a com.brainysoftware.applet package and my servlets in com.brainysoftware.servlet.

A package is not a physical object, and therefore you do not need to create one. To group a class in a package, use the keyword package followed by the package name. For example, the following MathUtil class is part of the com.brainysoftware.common package:

package com.brainysoftware.common;
public class MathUtil {
    ...
}

Java also introduces the term fully qualified name, which refers to a class name that carries with it its package name. The fully qualified name of a class is its package name followed by a period and the class name. Therefore, the fully qualified name of a Launcher class that belongs to package com.example is com.example.Launcher.

A class that has no package declaration is said to belong to the default package. For example, the Employee class in Listing 4.1 belongs to the default package. You should always use a package because types in the default package cannot be used by other types outside the default package (except when using a technique called reflection). It is a bad idea for a class to not have a package.

Even though a package is not a physical object, package names have a bearing on the physical location of their class source files. A package name represents a directory structure in which a period in a package name indicates a subfolder. For example, all Java source files in the com.brainysoftware.common package must reside in the common directory that is a subdirectory of the brainysoftware directory. In turn, the latter must be a subdirectory of the com directory. Figure 4.6 depicts the folder structure for a com.brainysoftware.common.MathUtil class.

Figure 4.6: The physical location of a class in a package

Compiling a class in a non-default package presents a challenge for beginners. To compile such a class, you need to include the package name, replacing the dot (.) with /. For example, to compile the com.brainysoftware.common.MathUtil class, change directory to the working directory (the directory which is the parent directory of com) and type

javac com/brainysoftware/common/MathUtil.java

By default, javac will place the result in the same directory structure as the source. In this case, a MathUtil.class file will be created in the com/brainysoftware/common directory.

Running a class that belongs to a package follows a similar rule: you must include the package name, replacing . with /. For example, to run the com.brainysoftware.common.MathUtil class, type the following from your working directory((the directory which is the parent directory of com).

java com/brainysoftware/common/MathUtil