Java Packages


In the previous tutorial, the name of each instance class was taken from the same namespace. This meant that a unique name had to be used for each class to avoid name conflicts. After a while, without any way to manage the namespace, you can omit convenient, descriptive names for individual classes. You also need some way of ensuring that the name you choose for the class will be reasonably unique and will not match the class name chosen by other programmers. Java provides a system for dividing class namespaces into more manageable parts. This process is a package.


Defining a Package

Creating a package is fairly easy: just include the package command as the first statement in the Java source file. Any class declared in this file includes a specific package. The package statement defines a place where classes are stored. Normally the names of the classes we create are placed in the default package which has no name. In short, the default package is fine for sample programs, but it is inadequate for real applications. Most of the time, you will specify a package for your code.

This is a common form of package statement:


package p1;

Here, p1 is the name of the package. For example, the following statement creates a statement call mypkg:


package mypkg;

Uses file system directories to store Java packages. For example, the class files for the class you declared as part of Mypkg should be in a directory named MyPkg store. Note that the case is significant, and the name of the directory must match the name of the package. Multiple files can contain the same package statement. The package statement specifies only the classes defined in a file. It does not exclude other categories in other files from being part of the same file package. Most of the time real-world packages are spread across many files.

You can create a hierarchy of packages. To do this, separate each package name using a period from the one above. The general form of the multipurpose package statement is shown here:


package p1[.p2[.p3]];

A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as


package p1.p2.p3;


Finding Packages and CLASSPATH

First, by default, the Java run-time system uses the current running directory as its starting point. So, if your package is in a subdirectory of the current directory, it will be available. Second, you can specify a directory path or paths by setting the CLASSPATH environment variable. Third, you can use the Java and Javac classpath options to specify your classpath. It is important to note that, starting with JDK 9, the package may be part of the module and is available on the way to the module.

For example, consider the following package specifications:


package pkg;

For a program to find pkg, the program can be executed from a directory immediately above pkg, or the CLASSPATH must be set to include the path to pkg, the -classpath option must specify the path to pkg when the program is run via Java.

When the second two options are used, the classpath must not include pkg, itself. It must simply specify the path to pkg. For example, in a Windows environment, if the path to pkg is


C:\JavaPrograms\Java\pkg

then the class path to pkg is


C:\JavaPrograms\Java

A Short Package Example

Program
package pkg;

class Employee
{
  String _name;
  int _salary;
  Employee(String name,int salary)
  {
    this._name=name;
    this._salary=salary;
  }
  void ShowEmployeeDetails()
  {
    System.out.println("Employee Details");
    System.out.println("Name : "+this._name);
    System.out.println("Salary : "+this._salary);
   }
}
public class Example 
{
   public static void main(String[] args) 
   {
     Employee obj=new Employee("John",10000);
     obj.ShowEmployeeDetails();
   }
}