Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Packages in Java & How to Use Them?

Updated on 14 November, 2022

9.74K+ views
7 min read

Packages in Java are used to group related classes, interfaces, and sub-packages. We use Java packages to avoid naming conflicts since there can be two classes with the same name in two different packages. Furthermore, searching, locating, and using interfaces, classes, annotations, and enumerations become easier with Java packages. 

Using the Java programming language to write software involves numerous individual classes, and it only makes sense to organise related classes and interfaces into packages. In a way, you can compare Java packages to the folders in a computer that we use to organise various files and keep our work clutter-free.

Check out our free courses to get an edge over the competition.

This guide will discuss the different types of packages in Java and how to use them.

Types of Packages in Java

Packages in Java are divided into categories based on whether the user defines them or not. The two kinds of Java packages are: 

  • Built-in packages
  • User-defined packages

1. Built-in packages

Predefined or built-in Java packages come along as part of the Java Development Kit (JDK) and consist of many predefined classes and interfaces that are part of Java APIs. Some of the commonly used built-in packages in Java are as follows:

  • java.io: Contains classes for supporting input/output operations.
  • java.lang: This in-built package is imported automatically and contains language support classes.
  • java.util: Contains utility classes for implementing data structures such as dictionary and support, linked list, date and time operations, etc.
  • java.net: Contains classes that support network operations.

Check out upGrad’s Advanced Certification in DevOps

We have to use the import statement to import any in-built Java packages and use the classes it contains. 

A simple example to show the use of the ArrayList class of Java.util package is as follows:

package Example;

import java.util.ArrayList;
class BuiltInPackage {
    public static void main(String[] args) {
        ArrayList<Integer> myList = new ArrayList<>(3);
        myList.add(3);
        myList.add(2);
        myList.add(1);
        System.out.println(“The list has the elements: ” + myList);
    }
}

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

Output:

The list has the elements: [3, 2, 1]

In the above example of Java.util.ArrayList, Java is the top-level package, util is a sub-package, and ArrayList is a class present in the sub-package util.

Importing packages in Java

As shown in the earlier example, Java has an import statement that lets us import an entire package or use only certain interfaces and classes defined in the package.

The general syntax of the import statement is:

  • import package.name.ClassName; //Imports a certain class only
  • import package.name.* //Imports the whole package

For example, import java.util.Date; imports only the Date class whereas import java.io.*; imports everything the java.io package contains.

However, the import statement is optional in Java, and if we want to use the class or interface from a certain package, we can use its fully qualified name as follows:

class MyClass implements java.util.Date {
    //body
}

2. User-defined packages

As evident from the name, user-defined packages are created by the user for grouping classes, interfaces, and sub-packages. 

Creating a user-defined package

To create a user-defined package in Java, we will choose a name for our package and use the package command as the starting statement in the Java source file. As an example, we will create a package named ExamplePackage using the following statement:

package ExamplePackage;

The package statement specifies the package to which the classes we define will belong. If we do not use the package statement, the class names will be put into the default package in Java which has no name.

 Learn Online software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Creating a class in the Java package 

Once we have created the package, the next step is to create a class inside the package. For this, we will declare the package name in the first statement of the program, followed by including the class as part of the package. 

Given below is a simple program to show how to create a class in a Java package. In the given an example, we create a class Calculate inside the package ExamplePackage.

package ExamplePackage;
public class Calculate {
   public int add(int p, int q){
return p+q;
   }
   public static void main(String args[]){
Calculate obj = new Calculate();
System.out.println(obj.add(70, 12));
   }
}

Output: 82

Java stores packages in file system directories. So, the above program will be saved in a file as Calculate.java and stored in the directory named ExamplePackage. When the file is compiled, Java will create a ‘.class’ file and store it in the ExamplePackage directory. 

Now, you can use the ExamplePackage package in another program. Here’s how:

import ExamplePackage.Calculate;
public class Demo{
   public static void main(String args[]){
Calculate obj = new Calculate();
System.out.println(obj.add(200, 523));
   }
}

Output: 723

In the above example, we have imported the package ExamplePackage to use the class Calculate. Importing the package as ExamplePackage.Calculate imports only the Calculate class. However, if you have more than one class inside the ExamplePackage package, you can use all the classes of the package by using the following import statement:

import ExamplePackage.*;

Creating a class inside a package while importing another package

To create a class inside a package while importing another package, we will begin by declaring the package name and then importing the other package. The following example demonstrates the same:

package OtherPackage; //Declaring a package
import ExamplePackage.Calculate; //importing a package
public class Example{
   public static void main(String args[]){
Calculate obj = new Calculate();
System.out.println(obj.add(300, 700));
   }
}

Output: 1000

In the above example, we declare the package OtherPackage, import the class Calculate from the package ExamplePackage, and then create the class Example.

Importing a class using a fully qualified name

We can avoid the import statement by using a fully qualified name. Using a fully qualified name makes only the declared class of the package accessible. But we need to use a fully qualified name every time we access the class or interface. Importing a class using a fully qualified name comes in handy when two packages have the same class name. 

The following example illustrates the same:

1. Calculate.java

package ExamplePackage;
public class Calculate {
   public int add(int x, int y){
return x+y;
   }
   public static void main(String args[]){
Calculate obj = new Calculate();
System.out.println(obj.add(20, 50));
   }
}

2. Demo.java

package OtherPackage; //Declaring a package
public class Demo{
   public static void main(String args[]){
ExamplePackage.Calculate obj = new ExamplePackage.Calculate(); //Using fully qualified name instead of import
System.out.println(obj.add(200, 100));
   }
}

Output: 300

In the example below, we have used the fully qualified name ExamplePackage.Calculate to create the object of it instead of importing the package. 

3. Sub-Packages in Java

Now that we have an idea about Java packages and how to create them let us understand the concept of sub-packages in Java.

A sub-package is a package inside another package and is used to categorise the package further. In other words, if we create a package multiply inside ExamplePackage package, then multiply is the sub-package. Thus, if we want to create a class within this sub-package multiply, then the sub-package should have a declaration at the start.

The following program demonstrates the concept of a Java sub-package:

package ExamplePackage.multiply; //Declaring the sub-package
public class Multiplication {
int product(int p, int q){
return p*q;
}
}

Now, if we want to use the Multiplication class, we can either: 

  1. Import it using the statement import ExamplePackage.multiply; or 
  2. use fully qualified name like ExamplePackage.multiply.Multiplication obj = new ExamplePackage.multiply.Multiplication();

Way Forward

Packages in Java can either be built-in packages or user-defined and are essential for better access and management of code. The concept of Java packages is crucial for anyone in the software development field. 

upGrad’s Job-linked PG Certification in Software Engineering is a 5-month online program that can help you master programming skills and prepare for software roles. The program focuses on pivotal skills such as Java, JavaScript, CSS3, HTML5, etc., and is specially designed for freshers who want to learn to program.

Program Highlights:

  • Specialisation in MERN/Cloud-Native
  • 500+ content hours
  • 50+ live sessions
  • 350+ hours of hands-on training
  • Five industry projects
  • 360-degree learning support
  • Networking with industry experts

Sign up today and learn from the best!

Frequently Asked Questions (FAQs)

1. What is a package and its types?

A package is a collection of related Java entities such as classes, subclasses, interfaces, annotations, and enumerations. It can also contain sub-packages. Packages in Java are of two types - built-in packages and user-defined packages.

2. Why do we use packages in Java?

We use packages in Java to prevent naming conflicts, make searching, locating, and using classes, interfaces, annotations, and enumerations easier, control access, and data encapsulation.

3. What is package-level access in Java?

Package level access in Java is the default access level provided by Java in case no access modifier is specified. The role of access modifiers is to restrict accessibility to a class, variable, or method to which it applies.

RELATED PROGRAMS