Java – Packages and its types with example5 min read

Packages in Java are a way to pack(group) a number of related types such as classes, interfaces, enumerations, and annotations together into a single unit. Think of it as a container for the classes.

Packages are used to avoid name conflicts, provide a good structure to the projects with maintainable code that consists of many classes.

Reason to use Packages(advantages):

  • It provides the reusability of the same code again and again in the program by creating a class inside a package for that particular task.
  • The name of the two classes can be defined by the same name in two different packages and we can avoid name conflicts using packages.
  • We can hide the classes in packages if the user doesn’t want it to be accessed by other packages.
  • Hundreds of classes used in packages are organized in a better way so that when needed it can be easily accessed by the programmer.

Creating Package in Java:

We can create a package in java by declaring the keyword ‘package’ followed by the ‘packageName‘.

Syntax to compile the program.

The -d in here specifies the destination/directory to put the generated class file.
Other directory names can be used such as d:/xyz (in case of windows) etc.
And to keep the package within the same directory we need to use. (dot).

Syntax to run the Java Program:

Let us see it in an example:

To compile:

To run:


import Keywords:

If the user needs to access or use the class of some other package in his/her current package, then it can be done with the help of the import keyword. import keyword imports the packages with reference to all classes or a particular class of that imported package.

This keyword is used to import both Built-in Packages and User-defined Packages. You can see the use of import below in examples.

Accessing Classes from Packages:

It can be done in the following ways,

1. Using fully qualified name:

When we use a fully qualified name then we do not need to use import but need to use a fully qualified name every time we want to access the class or interfaces of that other package.

Example of Using fully qualified name in Java:


2. import only selected class from a package:

This is the way in which the user can only access those classes that are required from the package. This is done by import packageName.className

Example of importing only selected class from a package in Java:


3. import all the classes from a selected package:

‘*’ is used instead of className when the package is imported. But the classes and packages that are present inside the sub-packages cannot be accessed. This can be done by import packageName.*;

Example of import all the classes from a selected package in Java:



The package within another package is called sub-package.

Example of Sub-packages in Java:

If we create a package called ‘Add’ inside a package named ‘AddNumber’ then the Add is called sub-package and it will be declared as package AddNumber.Add

And to use this class present in a sub-package we can either import the package or use fully qualified name.

Importing a package:

using a fully qualified name:

Categories of packages:

  • Built-in Packages (Java API)
  • User-defined Packages (self-created)

1. Built-in Packages (Java API) :

These are already defined packages in Java. Java APIs are the already defined classes that are provided for free use.

The mostly use built-in packages are:

  • java.lang
    Contains classes for primitive data types, strings, math operations, threads, and exception.
  • java.util
    contains utility classes such as vectors, hash tables, dates, etc.
    Stream classes for input/output operations.
  • java.awt
    Contain classes for implementing GUI such as buttons, menus, etc.
    Classes for networking operations.
  • java.applet
    Contain classes for creating and implementing applets.

Example: If the user input is required, we import Scanner and following is the way to do it:

2. User-defined Packages (self-created):

These packages are created/defined by the user themself. Users need to create a directory ‘egPackage‘ and name should match with the name of the package. Then the user can create a class (EgClass) inside that directory by declaring the first name as the package name.

And now the user can use this package in another program by importing it.



Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More