The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Packages in Java

Packages in Java are a way of grouping similar types of classes / interfaces together. It is a great way to achieve reusability. We can simply import a class providing the required functionality from an existing package and use it in our program. A package basically acts as a container for group of related classes. The concept of package can be considered as means to achieve data encapsulation. A package may consists of a lot of classes but only few needs to be exposed as most of them are required internally. Thus, we can hide the classes and prevent programs or other packages from accessing classes which are meant for internal usage only.
Packages are categorized as :
1 ) Built-in packages ( standard packages which come as a part of Java Runtime Environment )
2 ) User-defined packages ( packages defined by programmers to bundle group of related classes )

Built-in Packages
These packages consists of a large number of classes which are a part of Java API. For e.g, we have used package previously which contain classes to support input / output operations in Java. Similarly, there are other packages which provides different functionality.
Some of the commonly used built-in packages are shown in the table below :

Package NameDescription
java.langContains language support classes ( for e.g classes which defines primitive data types, math operations, etc.) . This package is automatically imported.
java.ioContains classes for supporting input / output operations.
java.utilContains utility classes which implement data structures like Linked List, Hash Table, Dictionary, etc and support for Date / Time operations.
java.appletContains classes for creating Applets.
java.awtContains classes for implementing the components of graphical user interface ( like buttons, menus, etc. ).
java.netContains classes for supporting networking operations.

Accessing classes in a package
Consider the following statements :

1 ) import java.util.Vector; // import the Vector class from util package

2 ) import java.util.*; // import all the class from util package

First statement imports Vector class from util package which is contained inside java package.
Second statement imports all the classes from util package.

User-defined Packages
Now, we will see how to create packages and use them. Suppose, we want to create a package named relational which contains a class named Compare. First, we will create a directory named relational ( the name should be same as the name of the package ). Then create the Compare class with the first statement being package relational;. Now, the package is ready to be imported. Following program shows the creation and use of Compare class.

Create 'Compare' class inside 'relational' package
package relational; /* name of the package must be mentioned.
                * this must be same as the directory under
                * which this file is saved */
public class Compare {
   public int getMax(int x, int y) {
      if ( x > y ) {
         return x;
      else {
         return y;
Use 'Compare' class to get the maximum of two values
import relational.Compare; /* import 'Compare' class from
                            * 'relational' package */
public class PackageDemo {
   public static void main(String args[]) {
      int val1 = 7, val2 = 9;
      Compare comp = new Compare();
      int max = comp.getMax(val1, val2); // get the max value
      System.out.println("Maximum value is " + max);

Please note that must be saved outside relational directory ( folder ) and must be saved inside relational directory since it is a part of the package.

Adding a class to a Package
We can add a class to an existing package by using the package name at the top of the program as shown above and saving the .java file under the package directory. We need a new java file if we want to define a public class, otherwise we can add the new class to an existing java file and recompile it. Similar to classes, we can also define interfaces as a part of a package and import and implement it as and when required. Moreover, we can extend a class inside a package to create subclasses.

A package created inside another package is known as a subpackage. When we import a package, subpackages are not imported by default. They have to be imported explicitly. In the following statement :

import java.util.*;

util is a subpackage created inside java package.

Using Static Import
The static import feature was introduced in Java 5. It facilitates accessing any static member of an imported class directly i.e without using the class name.
Following program differentiates between import and static import :

import the ' Math ' class in ' java.lang ' package
import java.lang.Math; // import 'Math' class
public class ImportDemo {
   public static void main(String args[]) {
      double val = 64.0;
      double sqroot = Math.sqrt(val); // Access sqrt() method using classname
      System.out.println("Sq. root of " + val + " is " + sqroot);
static import all the members of ' Math' class
import static java.lang.Math.*; // static import
public class StaticImportDemo {
   public static void main(String args[]) {
      double val = 64.0;
      double sqroot = sqrt(val); // Access sqrt() method directly
      System.out.println("Sq. root of " + val + " is " + sqroot);

In the above programs, sqrt( ) is a static method of Math class.

Back | Next