Working With Package in Java


This article describes what a package in Java is and where and how it is used. Packages are similar to namespaces in C#.


A package is a grouping of related types providing access protection and namespace management. Note that types refer to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.

Benefit of package

  1. A Java package is a mechanism for collecting Java classes into a folder (package). And the folder is compressed.
  2. It is used to provide a large number of APIs in a hierarchical form and makes your program lightweight. Because if all the APIs were provided in a single package then when you import that package into your program, your program would be heavyweight.
  3. And according to your needs, you make your own package and store the class file within your package.

Thus we can say that

  • Every class is part of a package.
  • All classes in a file are part of the same package.
  • You can specify the package using a package declaration:
    package name as the first (non-comment) line in the file.
  • Multiple files can specify the same package name.
  • You can access public classes in another (named) package using:
  • You can access the public fields and methods of such classes using:
  • You can avoid having to include the package-name using:
    1. import package-name.*;    
    1. import package-name.class-name;  
    at the beginning of the file (after the package declaration). The former imports all of the classes into the package, and the second imports just the named class.
  • You must still use: Class-name to access the classes in the packages, and
    class-name.field-or-method-name to access the fields and methods of the class; the only thing you can leave off is the package name.

Some core packages of Java are:

Package Name  Description
java.lang Basic language functionality and fundamental types
java.util Collection data structure classes File operations
java.math Multiprecision arithmetics
java.nio The New I/O framework for Java Networking operations, sockets, DNS lookups Key generation, encryption and decryption
java.sql Java Database Connectivity to access databases
java.awt The basic hierarchy of packages for native GUI components
javax.swing Hierarchy of packages for platform-independent rich GUI
java.applet Classes for creating an applet

Create It's Own package

For creating a package we use the following steps.

Defining a Package

To define a package, include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. If you omit the package statement then the class names are put into the default package, that has no name. This is the general form of the package statement:
package packageName;

How to create a hierarchy of packages 

To create a hierarchy of packages, separate each package name from the one above it with a period. The general form of a multilevel package statement:
package pkg1[.pkg2[.pkg3]];
In this example, we create a folder named Package and save this Java file with the name The package name and folder name should be the same:
  1. package Package;  
  3. public class OwnPackage  
  4.  {  
  5. public static void main(String args[])  
  6.    {  
  7.     System.out.println("this Example of creating own package");  
  8.     System.out.println("Myself Abhishek dubey");  
  9.     System.out.println("you can put n number of class in your package");  
  10.     System.out.println("this is enough to under stand package");  
  11.    }  
  12. }   
After running this code the output will be as follows:

How to Use package

For using the Java core packages we import the packages and for importing we use the import statement. In a Java source file, import statements occur immediately following the package statement and before class definitions.
This is the general form of the import statement:
import pkg1[.pkg2].(classname.*);
(*) means including all classes under that package.
For example   
  1. import java.lang.*;   
In this example, we are going to import the util package to use the class, as in:
  1. import java.util.Date;  
  3. public class PackageDemo  
  5. {  
  6.  public static void main(String arg[])  
  7.    {  
  8.   System.out.println(new Date());  
  9.   System.out.println("My self Abhishek dubey");  
  10.   System.out.println("Its example How we import java package");  
  11.    }  
  12. }   
After running the preceding application we'll find the following output: