A package is, essentially, a grouping of classes. A package is a namespace that organizes a set of related classes and interfaces

Defining a Package

To create a package is quite easy: simply include a package command as the first
statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If the package statement is omitted then, the class names are put into the default package, which has no name. While the default package is fine for short, sample programs, it is inadequate for real applications. The programmer will define their own package for their code.

This is the general form of the package statement:

package pkg;

Here, pkg is the name of the package. For example, the following statement creates a package called MyPackage.

package MyPackage;

Java uses file system directories to store packages. For example, the .class files for any class that is declared to be part of MyPackage must be stored in a directory called MyPackage. The directory name must match the package name exactly.

More than one file can include the same package statement. The package statement simply specifies to which package the classes defined in a file belong. It does not exclude
other classes in other files from being part of that same package. Most real-world packages are spread across many files.

A package hierarchy must be reflected in the file system of the Java development system. For example, a package declared as package java.awt.image; needs to be stored in java/awt/image, java\awt\image, or java:awt:image on the UNIX, Windows, or Macintosh file system, respectively. The package name must be chosen carefully. The  package cannot be renamed without renaming the directory in which the classes are stored.

Finding Packages and CLASSPATH

For example, consider the following package specification.

package MyPack;

In order for a program to find MyPack, one of two things must be true. Either the
program is executed from a directory immediately above MyPack, or CLASSPATH must be set to include the path to MyPack. The first alternative is the easiest (and doesn’t require a change to CLASSPATH), but the second alternative lets the program finds MyPack no matter what directory the program is in.

A Short Package Example

package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");

System.out.println(name + ": $" + bal);
}
}
class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
Call this file AccountBalance.java, and put it in a directory called MyPack.Next, compile the file. Make sure that the resulting .class file is also in the MyPack directory. Then try executing the AccountBalance class, using the following command line:

java MyPack.AccountBalance

Importing Packages

Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to write a complete Java program. If you are going to refer to a few dozen classes in your application, however, the import statement will save a lot of typing.

In a Java source file, import statements occur immediately following the package
statement (if it exists) and before any class definitions. This is the general form of the
import statement:

import pkg1[.pkg2].(classname|*);

 

TestBalance imports MyPack and is then able to make use of the Balance class:

import MyPack.*;
class TestBalance {
public static void main(String args[]) {
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show();}

}