The Role of Packages in Java

Introduction

 It is possible to run out of unique names for classes in a big programming environment. There may be situations when two or more persons use the same name for their classes. This will result in chaos.
Java provides us with a feature called package to avoid this problem.

Packages contain a set of classes in order to ensure that the class name is unique. Packages are containers for classes that are used to compartmentalize the class namespace. Packages are stored in a hierarchical manner and are explicitly imported into a new class definition. A period is utilized as a separator.

For example, if there exists a class called Address within a package called MyPackage which exists within another package known as BasePackage, the first statement of the class is specified as.
package BasePackage.MyPackage.Address

CLASSPATH Variable

Whenever a class is created without any package statement, it is implied that the class is included in Java’s default package; in the sense of writing small programs, it is not necessary for the package name to be mentioned. The necessity for packages can be understood in the real-time application environment.

The Java compiler and the interpreter search the .class files in the path specified in the CLASSPATH Environment variable. The current working directory is, by default, included in this variable. Hence, it is possible to compile and execute the programs from the existing directory. In the case of creating a package, ensure that the root directory of the package is included in the CLASSPATH variable.

Creation of a Package

The following steps are involved in the creation of a package.

  • Create a directory, which has the same as the package
  • Include the package command, along with the package name, as the first statement in the program
  • Write class declaration
  • Save the file in this directory as NameofClass.java, where NameofClass is the name of the class.
  • Compile this file using javac
  • There are two ways of using this program- change to the previous level directory and use Java packageName.ClassName or set the CLASSPATH variable accordingly.

Code illustrates the creation of sample package and sub-package & their usage of the same in a program.

  • Create a directory called pack.

Open a new file and enter the following code.

package pack;
public class class1
{
public static void great()
{
System.out.println(“Hello Ashish”);
}
}

Save this file as Class1.java inside the pack directory.

  • A package called pack has now been created, which contains a class class1
  • Create a Subdirectory called subpack inside the pack directory

Open a new file and enter the following code.

package pack.subpack
     public class class2
      {
	public static void farewell()
	{
	System.out.println(“Hello Students”);
	}
     }

Save this file as class2.java inside the subpack directory.

  • A subpackage called subpack has now been created with class class2 in it.

Open a new file and enter the following code.

import pack.*;
import pack.subpack.*;
class Importer
{
public static void main(String args[])
{
class1.great();
class2.great();
}
}
  • Save the file Importer.java in the root directory of the package
  • Compile the file using javac Importer.java
  • Run the file using Java Importer

Below Figure shows the directory structure of the above mentioned package.

Subpack directory

Output

cmd prompt

Summary

Packages contain a set of classes in order to ensure that their class name is unique. Packages are containers for classes that are used to compartmentalize the class namespace.


Similar Articles