Introduction

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

Definition

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:
    package-name.class-name
  • You can access the public fields and methods of such classes using:
    package-name.class-name.field-or-method-name
  • You can avoid having to include the package-name using:
    import package-name.*; or
    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
java.io File operations
java.math Multiprecision arithmetics
java.nio The New I/O framework for Java
java.net Networking operations, sockets, DNS lookups
java.security Key generation, encryption and decryption
java.sql Java Database Connectivity to access databases
java.awt 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:

Syntax

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:

Syntax

package pkg1[.pkg2[.pkg3]];

Example

In this example we create a folder named Package and save this Java file with the name OwnPackage.java. The package name and folder name should be the same:

OwnPackage.java

package Package;

 

public class OwnPackage

 {

public static void main(String args[])

   {

    System.out.println("this Example of creating own package");

    System.out.println("Myself Abhishek dubey");

    System.out.println("you can put n number of class in your package");

    System.out.println("this is enough to under stand package");

   }

}

OUTPUT


After running this code the output wil be as follows:

Clipboard01.jpg

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:

Syntax

import pkg1[.pkg2].(classname.*);
(*) means including all classes under that package.

For example

import java.lang.*;

Example

In this example we are going to import the util package to use the class, as in:

import java.util.Date;

 

public class PackageDemo
{

 public static void main(String arg[])

   {

  System.out.println(new Date());

  System.out.println("My self Abhishek dubey");

  System.out.println("Its example How we import java package");

   }

}

Output

After running the preceding application we'll find the following output:

Clipboard02.jpg