Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Import Package in Java?
Next article icon

How to Import Package in Java?

Last Updated : 13 Jun, 2025
Comments
Improve
Suggest changes
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.

Next Article
How to Import Package in Java?

M

musklf2s
Improve
Article Tags :
  • Java
  • Java-Packages
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences