How to Import Package in Java?

Last Updated : 30 Jul, 2025

When we write Java programs, sometimes we need to use other classes that are not part of the default package and that's why packages come into use. Packages are like folders that group related classes together.

What is a Package?

In Java, a package is a collection of classes and interfaces that are grouped together. For example, the java.util package has classes like ArrayList, Scanner and many others that we can use every day.

Importing a Package in Java

There are two main ways to import in Java, which are listed below:

  • Import a specific class
  • Import all classes in a package

Now we are going to discuss each one of them in detail

1. Import a Specific Class

We can import a single class from a package using the syntax below.

Syntax:

import package_name.ClassName;

Example: To import the ArrayList class from java.util we have to write like this

import java.util.ArrayList;

2. Import All Classes in a Package

We can import all classes in a package using the asterisk * wildcard which is shown below.

Syntax:

import package_name.*;

Example: This imports all classes from the java.util package, such as ArrayList, HashMap, Date, etc.

import java.util.*;

Note: Wildcard imports (*) do not include sub-packages and may cause conflicts if multiple packages contain classes with the same name.

Types of Packages in Java

In Java, packages are of two types which arel listed below:

1. Built-in Packages: These are pre-defined packages provided by the Java Standard Library. This package contains commonly used classes and functions that saves a lot of time while coding. For example java.util includes classes like ArrayList, HashMap and Date to help manage collections of data.

2. User-defined Packages: These are the packages that we create ourselves to organize our code in a way that makes sense for our project. For example, we have created a package for handling user authentication or for managing database connections.

Note: With the help of packages, we can keep our code organized, reusable and easier to maintain.

Now, we are going to create a user defined package in Java for better understanding.

Creating a User-defined Package in Java

In large applications, it's important to organize classes into packages for better structure and clarity. Here's how to create and use user-defined packages in Java

1. Define a Package: Our first step is to define a package at the top of our Java file, we can do by using package keyword.

Example:

package mypackage;

Note: This will define a package named as mypackage. Now any class which is defined in this file will basically belong to this package.

2. Use the Package: To use a class from a user-defined package, we need to import it into our program he syntax is similar to importing built-in packages.

Example:

import mypackage.MyClass;

How to Use a User Defined Package?

Now geeks, we are going to understand the steps how we can use a user defined package in Java

Step 1: Create the Package

Create a Java file named MyClass.java and write the following code which is listed below:

Java
package mypackage;

public class MyClass {
    public void greet() {
        System.out.println("Hello from MyClass in mypackage!");
    }
}

Step 2: Import and Use the Class

Now, in another Java file Main.java, we can import and use the class from mypackage as follows:

Java
import mypackage.MyClass;

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.greet();  
    }
}

Why to Use Package in Java

The reasons to use packages are listed below:

  • Packages group related classes logically.
  • Packages avoid name conflicts between classes.
  • Packages control code accessibility through access modifiers
Comment