Java

Packages in Java | Explained

A java package is nothing but a procedure of grouping different classes, interfaces, enumerations, etc. The primary purpose of grouping different classes, interfaces, etc. under a package is to manage the space and to avoid naming conflicts. We can understand the concept of java packages by means of folders in windows where a single folder can hold multiple files. In java, packages are either predefined or user-defined.

This write-up will provide a profound understanding of the following concepts regarding Java packages:

Let’s start!

Built-in Packages in Java

Java provide a wide range of built-in packages such as java.util, java.lang, etc. that can be used to achieve different functionalities. We can import either a single class of a package or a whole package. To avail the functionalities of the built-in packages, we have to import the package, and to do so import keyword is used.

In order to import a single class of a specific package, we have to utilize the following syntax:

import packageName.subPackageName.ClassName;

Following will be the basic syntax to import the entire package:

import packageName.subPackageName.*;

For the profound understanding of concepts consider the below-given examples:

Example

Let’s consider we want to take input from the user and to do so we need to import the scanner class of java.util package:

import java.util.Scanner;

The above line of code represents that Scanner is a class that belongs to java.util package.

Example

Let’s consider another example where we want to avail the functionalities of ArrayList class then we can

import the built-in ArrayList class as follows:

import java.util.ArrayList;

The above snippet shows that import the ArrayList class that exists in java.util package.

Example

Let’s consider we have to utilize multiple classes of a specific package then instead of importing every single class individually, we can import the entire package in one go as shown below:

import java.util.*;

The above code snippet elaborates that import every single class of java.util packge.

Similarly, we can import any built-in class or the entire package.

Example

Let’s consider the below-given code snippet for the profound understanding of how to import and utilize the built-in packages in java:

import java.util.Scanner;

public class UsersInput {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  
        System.out.println("Please Enter the Name of Employee: ");
        String empName = scan.nextLine();
        System.out.println("Please Enter the ID of Employee: ");
        int empId = scan.nextInt();
        System.out.println("Employee Name :" + empName);
        System.out.println("Employee ID :" + empId);

    }

}

The complete code and its descriptive screenshot are provided below:

The above snippet verifies that the built-in class is imported successfully as it takes the input from the user successfully.

User-defined Packages in Java

Java allow us to create our own packages and to do so we need to follow the following steps:

First Step

In the very first line of our source file we will utilize the package keyword:

package packageName;

Once the package is created then we can create any number of classes as we want:

Second Step

Next we will declare and define the class under the package:

package packageName;
public class className
{
//code
}

In the above snippet public is access specifier which indicates that this class is accessible to all other classes of the package.

Third Step

If you are not utilizing any IDE then you have to follow the below-given syntax to compile a java package:

javac -d . javaFileName

Here, in the above snippet -d is nothing but a keyword that represents the destination for the file to be saved. In the above snippet, we utilized a “.” which indicates that save the package in the same directory however, you can specify the directory name of your choice.

Fourth Step

In order to run the java package all you need to do is specify the full name:

java packageName.fileName

In this way we can create and utilize the user-defined java packages and classes within packages.

Practical implementation of User Defined Packages

Let’s consider the below example to understand the working of user-defined packages in java.

Example

The below snippet creates a package named users and creates a class within the package.

package users;
public class Users {

    public static void main(String[] args) {
        String empName = "Joe";
        int empId = 12;
        System.out.println("Employee Name :" + empName);
        System.out.println("Employee ID :" + empId);
    }
}

The complete code and respective output is shown in the below-given snippet:

The output verifies the working of the user-defined package. We can create multiple classes under the same package or we can import the different classes from other packages using the import keyword.

Conclusion

There are two types of packages in java i.e. built-in or user-defined both have the same goal of grouping the related classes under one package. Java packages assist us in managing the space, avoiding naming conflicts, and providing more access control. In java, we can import the build-in packages and create the user-defined packages using import and package keywords respectively. This write-up step by step guide for java packages.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.