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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More