Packages In Java

Google Sponsored Links

In general, Package is an item or group of items wrapped in a box. We generally use this terminology to describe Parcels. It is part of our daily life, for example, if we order anything from online store then item which we have ordered is termed as a package and similarly, we receive the notification like Your Package will be arriving in the next two days.

If you have ordered multiple items then those items can be delivered to you in a single or multiple packages, depending on your order type and shipping.

Java Packages also work on the same mechanism to encapsulate(enclose in) a group of classes, sub packages and interfaces in a single or multiple Package depending on your requirement.

We have two types of Java Packages: built-in packages (standard packages which come as a part of Java Runtime Environment) and the packages we can create (also known as a user-defined package).

Packages are used for:

  • Preventing naming conflicts.
  • Making searching/locating and usage of classes, interfaces, enumerations, and annotations easier.
  • Providing controlled access.
  • Packages can be considered as data encapsulation (or data-hiding).
  • In packages, classes can be declared uniquely compared with classes in other packages.
  • Packages also allow programmers to separate design from coding.

Built-in Packages :

These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are shown in the table below :

Google Sponsored Links
Package Name Description
java.lang  Contains language support classes ( for e.g classes which defines primitive data types, math operations, etc.). This package is automatically imported.  Contains classes for supporting input/output operations.
java.applet  Contains classes for creating Applets.
java.awt  Contains classes for implementing the components of graphical user interface (   like buttons, menus, etc. ).  Contains classes for supporting networking operations.
java.util  Contains utility classes which implement data structures like Linked List, HashTable, Dictionary, etc and support for Date / Time operations.

User-defined packages :

These are the java packages that are defined by the user. First, we create a directory then create the class inside the directory with the first statement being the package names.

Illustration of user-defined packages:

  • Creating our first java package:

package package_name;

package class ClassFirst {

public void methodClassFirst() {

System.out.println("Hello !!!!");
  • Creating our second package:

package package_First;

public class ClassSecond {

public void methodClassSecond(){

System.out.println("Hello !!! Hello !!!");
  • Making use of both the packages created above:

import package_name.ClassFirst;
import package_First.ClassSecond; 
public class Testing { 
	public static void main(String[] args){ 
		ClassSecond a = new ClassSecond(); 
		ClassFirst b = new ClassFirst(); 

Rules for Package :

  • Class stored within a package is called “packaged class”
  • Package class should start with: “package packagename;
  • To access packaged class outside the package, it should be declared as public.
  • A packaged class needs to compiled and executed from outside the package.

Compile :

D:\Data>javac mypack\

Interpret :

D:\Data>java mypack.MainProg

Note: Classes from the same package need not be imported.

  • Specific import:  This import overrides other types of import.

import mypack.mysubpack1.Abc;
  • In case of ambiguity inline import is used :

mypack.Abc obj1 = new mypack.Abc();
  • General import:

import mypack.mysubpack1.*;
  • To call classes from other drives/directories, CLASSPATH needs to be set:
D:\Data>set CLASSPATH=.;E:\data;C:\

NOTE: only the classes that are stored within package can be imported


package mypack;

import mypack.mysubpack1.Abc;

import mypack.mysubpack1.*;

import mypack.mysubpack2.*;

import mypack.mysubpack1.mysubsubpack1.*;

import mypack.mysubpack1.mysubsubpack2.*;

public class MainProg {

public static void main(String args[]) {

Abc obj = new Abc();

mypack.Abc obj1 = new mypack.Abc();

mypack.mysubpack2.Abc obj2 = new mypack.mysubpack2.Abc();

mypack.mysubpack1.mysubsubpack1.Abc obj3 = new mypack.mysubpack1.mysubsubpack1.Abc();

mypack.mysubpack1.mysubsubpack2.Abc obj4 = new mypack.mysubpack1.mysubsubpack2.Abc();

Efg obj5 = new Efg();

Lmn obj6 = new Lmn();

Pqr obj7 = new Pqr();

Xyz obj8 = new Xyz();

somepack.Abc obj9 = new somepack.Abc();

anotherpack.Abc obj10 = new anotherpack.Abc();

Find the working example of Package implementation of codedbug GitHub repo.

Google Sponsored Links