Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Importing Libraries | Section
Essential Java Concepts

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.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookImporting Libraries

Desliza para mostrar el menú

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.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
some-alt