Java Packages

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:

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 import keyword. import keyword imports the packages with reference to all class 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:

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:

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:

Output:


Sub-Packages:

The package within another package is called sub-package.
Example:
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: