What is a Java Package

Packages are containers for classes.
Packages are used to keep the class name space compartmentalized.
In Java, package is mapped to a folder on your hard drive.

Defining a Package

To define a package, 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.

If you omit the package statement, the class names are put into the default package, which has no name.This is the general form of the package statement:

package packageName;

Create a hierarchy of packages.

To create a hierarchy of packages, separate each package name from the one above it by use of a period. The general form of a multileveled package statement:

package pkg1[.pkg2[.pkg3]];

A package hierarchy must be reflected in the file system of your Java development system.

Java package maps to directory

A Short Package Example

package MyPack;

public class Main {
  public static void main(String args[]) {

Then try executing the class, using the following command line:

java MyPack.Main
  Java Book 

  1. What is a Java Package
  2. Importing Packages
  3. static import
  4. Source Files