Open In App

How to Import Package in Java?

Last Updated : 13 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Now, Geeks, you must be wondering how we actually use those classes in our code, and this is where importing packages comes into play.

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;

Note: Now, we can use ArrayList directly in the code without writing 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 does not import sub-packages.
  • Importing all classes may create some problems if classes with the same name exist in multiple packages.


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

Whenever we write big applications, it's our responsibilty as a developer to organize classes in packages for better clarity and understanding. Here’s how we can 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();  // Output: Hello from MyClass in mypackage!
    }
}

Why to Use Package in Java

The reasons to use packages are listed below:

  • Packages help keep our classes organized by grouping them logically.
  • Without packages, it is very easy to run into issues when two classes have the same name.
  • Packages allow us to control which parts of our code is accessible to others.

Article Tags :
Practice Tags :

Similar Reads