Importing 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
1import 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
1import 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
123import 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.63
Importing 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
1import 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
1import 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
123import 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.
Thanks for your feedback!