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:

Syntax to compile the program.

Note:
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:

Output:


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:

Output:

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:

Output:

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:

Output:


Sub-Packages:

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.
  • java.io
    Stream classes for input/output operations.
  • java.awt
    Contain classes for implementing GUI such as buttons, menus, etc.
  • java.net
    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.

Output:

MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …