Packages in Java – Advantages, Types | How to Create and Import Packages in Java?

A package in java is defined as the group of related classes and interfaces into a single unit. For example: Suppose you want to perform database operations, the required classes and interfaces are grouped into a single unit which is java.sql package. Therefore we can say that packages are the encapsulation mechanism to group related classes and interfaces into a single unit. There are two types of packages in Java: built-in packages and user-defined packages.

Advantages of using Packages in Java

1. Naming Conflicts: There are 2 Date classes available in Java, one inside the SQL package and the second inside the util package. So, by using the packages we can able to differentiate java.util.date and java.sql.Date. If a package statement is not there then it is impossible to have 2 date classes. By using a package, we can able to resolve naming conflicts.

2. Modularity and Maintainability: As we know, Packages are a group of related classes and interfaces, therefore, Modularity and the Maintainability of the application are gone to be improved.

3. Security: Suppose you have a class inside a package pack1. Assume this class is not public if it is not public then an outside person not able to access this class because the default is only accessible within the same package. The package provides security to our component.

Types of Packages in Java

There are two types of packages in Java.

Packages in Java 1

1. Built-in Package

The packages which are already defined and part of Java API is known as Built-in packages. Some commonly used built-in packages are given below:

  • java.lang: This package contains language support classes.
  • java.util: This package contains utility classes such as LinkedList, Dictionary, etc.
  • java.io: This package contains classes for file read/write operations.
  • java.net: This package contains classes for networking operations.
  • java.applet: This package contains classes for creating the applets.

Syntax for Built-in Packages

import package.name.Class;   // Import a single class
import package.name.*;   // Import the whole package

2. User-Defined Package

The packages that are defined by the user is known as a user-defined package.

How to Create Packages in Java? | Creating a Package

Creating a package in Java is not a difficult task. You can create a package in java by using the package keyword. for example:

package MyPackage;

Here package is a keyword and MyPackage is a package name.

How to compile and run Java Package.
Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. for example:

package World;
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World");
}
}

Now compile this file as:

javac -d . HelloWorld.java

Here -d specifies the destination where to put the class file and . (dot) specifies the current working directory.

You can run this file as:

java World.HelloWorld

We need to use a fully qualified name to run the class. Here World is a package and HelloWorld is a class inside this package.

Import a Package | How to access the Package from Another Package?

There are three ways to access the package from another package.

  1. import package.*;
  2. import package.classname;
  3. using a fully qualified class name.

1. Using a Package Name

If we use package.* than all the classes and interfaces of this package accessible. We can use the import keyword to access the classes and interfaces of another package to the current package. Let’s see the example.

Example of Java package that imports the package-name.*;

//save by class1.java.
package pack1;
public class Class1{
public void display(){
System.out.println("Inside class1 method.")
}
}

//save by Class2.java
package pack2;
import pack1.*;
class Class2{
public static void main(String args[]){
Class1 obj = new Class1();
obj.display();
}
}

Output:

Inside class1 method.

2. Using the package.classname

We can use the import package.classname to access the only declared class of this package. Let’s see the example.

Example of the package in java using the package.classname

//save by class1.java.
package pack1;
public class Class1{
public void display(){
System.out.println("Inside class1 method.")
}
}

//save by Class2.java
package pack2;
import pack1.Class1;
class Class2{
public static void main(String args[]){
Class1 obj = new Class1();
obj.display();
}
}

Output:

Inside class1 method.

3. Using a Fully Qualified Class Name

If we use a fully qualified class name then only the declared class of this package will accessible and there is no need to import. In this case, we need to use a fully qualified name every time when you are accessing the class or interface.

Example of a package in Java using a fully qualified class name

//save by class1.java.
package pack1;
public class Class1{
public void display(){
System.out.println("Inside class1 method.")
}
}

//save by Class2.java
package pack2;
class Class2{
public static void main(String args[]){
//using fully qualified class name.
pack1.Class1 obj = new pack1.Class1();
obj.display();
}
}

Output:

Inside class1 method.

The Sequence of a Program must be in the below Order. First Package and then Import and then Class.

Sequence of a Program

How do Packages Work?

Package Names and Directory Structure is Closely related. For instance, if a package name is college.staff.ece then there are three directories namely college, staff, ece such that ece is present in staff and staff is present in college. Also, the college directory path is available through Classpath Variable. Parent Directory of College is stored in ClassPath just to make sure they are easy to locate.

Package Naming Conventions: Packages are named in reverse order of domains. For Example, in a College recommended conventions are as college.tech.it, college.tech.cse, college.art.history, etc. and so on.

Adding a Class to a Package: More Classes can be added to a Package by simply using the Package Name on the top of the program and saving it in the directory package. We need a new Java file in order to define a Public Class or else add new class to an existing .java file and then recompile it.

Subpackage in Java

Package within the Package is known as a Sub Package. It needs to be created to categorize the package further.

The Standard Form for defining a Package is given by domain.company.package

Java SubPackage Example Program

Output:Hello subpackage

Leave a Comment