Package: – Packages are java’s way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to functionality. In fact, packages act as container for classes. By organizing our classes into packages, we achieve the following benefits –

  1. The classes contained in the packages of another program can be easily reused.
  2. In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages have the same name. They may be referred by their fully qualified name, comprising the package name and the class name.
  3. Packages provide a way to hide classes thus preventing other programs or packages from accessing classes that are mint from internal use only.
  4. Packages also provide a way for separating design classes and decide they are relationship, and them we can implement java code needed for the method. It is possible to change the implementation of any method without affecting rest of the design.

For most application, we will need to use to different sets of classes, one for the internal representation of our program’s data and the other for external presentation purposes. We may have to build our own classes for handling our data and use existing class libraries for designing user interfaces. Java packages are there for classified into two types. The first categories are known as java API package and second is known as user define package.

Java API package: – Java API provide a large number of classes grouped into different packages according to functionality. Most of the time we use the packages available with the java API. Java API package divided into two parts namely java and package name both parts are linked with dot operator. Java package name and their utility are as fallows –

Package Name


java.lng Language support classes. These are classes that java compiler itself uses and there for they are automatically imported. They include classes for primitive type, string, math function, threads and exception.
java.util Language utility classes such as vectors, hash table, random number, date etc. Input / Output support classes. They provide facilities for the input or output of data.
java.awt Set of classes implementing graphical user interface. They include classes for windows, buttons, list, menus and show on. Classes for java networking. They include classes for communicating with local computers as well as with internet server.
java.applet Classes for creating and implementing applets.


User define package: – A user define package is different from API package an API package is defined in java library where as a system define package define by the user. We must first declare the name of the package using the package keyword followed by the package name. this must be the first statement in a java source file. Then we defined a class just as we normally a define a class. Syntax of user define package is –

package <Package Name>;
public class <Class Name>
                Body of a class

Here keyword package contains a package name and public class is now considered a part of this package. When we create this package, we can save it into disk as file and located in the same directory where source code is written and compiled. Java will create a .class file and store it in the same directory. Remember that the .class file must be located in a directory that has the same name as the package, and this directory should be a sub directory of the directory where the classes will import the package are located. To create our own package, fallow the following step –

  1. Declare the package at the beginning of file using the form.
  2. Define the class that is to be put in the package and declare it as public.
  3. Create a sub directory under the directory where the main source file is stored.
  4. Store the listing as the class name .java file in the sub directory created.
  5. Compile the file. This create .class file in the sub directory.

Accessing a package: – We can call a package using import statement. The import statement is used to call multiple packages which are user define. The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching a class is as fallows –

import <Package1>[.Package2.Package3].ClassName;

here package1 is the name of the top-level package, package2 is the name of the package that is inside the package1 and show on. We can have any number of package in a package hierarchy. Finally, the explicit class name is specified.