Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Importing Libraries | Deep Java Structure
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Java Classes and Core Mechanics

bookImporting Libraries

Using Pre-existing Libraries in Our Code in Java

You’ve already encountered libraries when learning about the JRE. A library extends a program’s functionality, and there are many available—you’ll even create your own in the future. To avoid loading unnecessary libraries and wasting memory, Java uses the import keyword to include only what is needed.

In Java, import allows you to use classes or packages from other sources without writing their full names each time. This makes the code cleaner, more readable, and easier to maintain. The syntax looks like this:

Main.java

Main.java

copy
1
import parent.Child;

The parent library is located higher in the hierarchy than the child library. For example, let's say we have a class called Person that we want to import, and it is located in the model package. To import it, we would use the syntax import model.Person; since the Person class is inside the model package.

We will learn more about classes and how to create them later in this course.

We also can import all child libraries using the following syntax:

Main.java

Main.java

copy
1
import parent.*;

Using .* is not considered a best practice as it adds additional memory overhead and affects performance in general. Instead, it is better to use multiple imports. In code, it will look like this:

Main.java

Main.java

copy
123
import parent.Child1; import parent.Child2; import parent.Child3;

This way, we can see which specific libraries we import and what we need to use. Additionally, we avoid unnecessary memory overhead and improve the performance of our application.

In the next chapter, we will explore the practical usage of the import keyword and import a library into our code.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookImporting Libraries

Swipe to show menu

Using Pre-existing Libraries in Our Code in Java

You’ve already encountered libraries when learning about the JRE. A library extends a program’s functionality, and there are many available—you’ll even create your own in the future. To avoid loading unnecessary libraries and wasting memory, Java uses the import keyword to include only what is needed.

In Java, import allows you to use classes or packages from other sources without writing their full names each time. This makes the code cleaner, more readable, and easier to maintain. The syntax looks like this:

Main.java

Main.java

copy
1
import parent.Child;

The parent library is located higher in the hierarchy than the child library. For example, let's say we have a class called Person that we want to import, and it is located in the model package. To import it, we would use the syntax import model.Person; since the Person class is inside the model package.

We will learn more about classes and how to create them later in this course.

We also can import all child libraries using the following syntax:

Main.java

Main.java

copy
1
import parent.*;

Using .* is not considered a best practice as it adds additional memory overhead and affects performance in general. Instead, it is better to use multiple imports. In code, it will look like this:

Main.java

Main.java

copy
123
import parent.Child1; import parent.Child2; import parent.Child3;

This way, we can see which specific libraries we import and what we need to use. Additionally, we avoid unnecessary memory overhead and improve the performance of our application.

In the next chapter, we will explore the practical usage of the import keyword and import a library into our code.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
some-alt