.

Java Tutorial: Packages in Java

Packages

  • It is the implementation reusability concept
  • It maintains huge collection of classes , interfaces
  • Similar kind of classes and interfaces are packed together in the name of packages

e.g.

  • lang is a builtin package
  • Steps involved in the creation of package

  • using the keyword package , package can be generated
  • Followed by package Keyword, package name has to be allotted
  • There must be a folder with the same name of a package
  • It should be positioned at the first line
  • Its a good programming practice to specify package name with all small case letters
  • For a group of classes, only a single package statement is permitted
  • The byte code file is stored in the package named folder
  • Any no. of import statements can be included in a program
  • Those statements are to be placed following the package statement but prior to classNamedeclaration

Syntax for the Creation of Package

{`

package packagename;

public classNameclasname

{

// clas definition;

}

`}

It is necessary to place the file inside the directory where in the name of the package and directory reflects the same

e.g.

...\packagename\clasname.java

When the beneath example is considered the main

 classname is MammalInt inside the folder animals

e.g.

 The classNameis considered as animals.MammalInt

The pathname is included or preceded with the package name

e.g.

The path is considered as animals/MammalInt.java

The Syntax for importing a package is

import Packagename.Classname – It imports specific Class  i.e. mentioned className(or)

import Packagename.* - It imports all classes in a given package

Example

packages img1
.