Java Package and JAR

Many times, when we get a chance to work on a small project, one thing we intend to do is to put all the Java files into one single directory. It is quick, easy and painless. Nevertheless, if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In Java, we can avoid this kind of problem by using the concept of Packages.

Packages help us organize files into different directories according to their functionality, usability as well as category they should belong to. Basically, files in one directory (i.e. package) would have different functionality from that of another directory. For example, files in java.io package do something related to I/O, but files in java.net package give us the way to deal with networking.

Packaging also helps us avoiding class name collision when we use the same class name as that of others. For example, if we are having a class name called “Stack”, its name would clash with the Stack class from JDK. Nevertheless, this won’t happen in Java because JDK use java.util as the package name for Vector class (java.util.Stack). So, our Stack class can be put into another package named com.example without conflicting with anyone.

The benefits of using package provides ease of maintenance, organization, and increase collaboration among Java developers. Understanding the concept of package will also help us manage and use files stored in Java Archive (JAR) files in more efficient ways.

 

QUICK SUMMARY

A package in Java groups a number of related classes and/or interfaces together into a single unit.

Basically, packages act as “containers” for classes and interfaces.

A package may also contain sub-packages.

 

Benefits of Packages

The benefits of having packages are:

1. Logical grouping. Packages provide a way to group related classes and interfaces into one single unit.

2. Naming conflict resolution. Using packages, the naming conflicts between class and interface names can be resolved.

3. Reusability of code. Packaging enhances reusability of code, a concept similar to “class libraries” in other languages (e.g. C, C++ etc.)

4. Providing access protection. Classes in packages can be hidden if we don’t want other packages to access them.

 

Java Foundation Packages

♦ Java provides a large number of classes groped into different packages based on their functionality.

♦ The six foundation Java packages are:

   1. java.lang : It contains classes for primitive types, strings, math functions, threads, and exception

   2. java.util : It contains classes such as vectors, hash tables, date etc.

   3. java.io : It denotes stream classes for I/O

   4. java.awt : It denotes classes for implementing GUI – windows, buttons, menus etc.

    5. java.net : It denotes classes for networking

    6. java.applet : It contains classes for creating and implementing applets

 

Using System-defined Packages

The packages are organized in a hierarchical structure. For example, a system-defined package named “java” contains the sub-package “awt”, which in turn contains various classes required for implementing GUI (Graphical User Interface). See the figure below.

Figure 1: A sample package with sub-package and classes

 

Accessing Classes from Packages

There are two ways of accessing the classes stored in packages:

        1. Using fully-qualified class name

                     ο java.lang.Math.sqrt(x);

        2. Import package and use class name directly

                     ο import java.lang.Math;

                     ο Math.sqrt(x);

Selected or all classes in packages can be imported:

                     import package.class;  // for system-defined and user-defined packages both

                     import package.*;        // for system-defined packages only

Implicit in all programs:  import java.lang.*;

The package statement must appear first in a Java program

 

In the next section we will learn how to create a user-defined package and use the classes under this package after creation.