How to Import Package in Java?
Last Updated : 13 Jun, 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.
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.
Similar Reads
Package Imports in JShell of Java 9 Package Imports By default, 10 packages are imported and can also be imported any package by using import statement. To see, default import packages, we can use following command. Importing java.sql package. Listing import packages and it will show available accessible packages. Now number of packag
1 min read
JEP Package Tool in Java J package tool was introduced as an incubation tool in java 14. It remained an incubation tool till java 15. Â It packages java applications into platform-specific features. Basically, this tool converts our java source file into an executable file. In Windows, the executable file is of two types .ex
3 min read
Static import in Java In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import
4 min read
How to Import Custom Class in Java? Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concep
3 min read
Built-in Packages in Java In Java, Packages are used to avoid naming conflicts and to control the access of classes, interfaces, sub-classes, etc. A package can be defined as a group of similar types of classes, sub-classes, interfaces, or enumerations, etc. Using packages makes it easier to locate or find the related classe
7 min read