4.14 Basic Java Syntax: Importing Libraries

When you're learning to program in Java, one of the most powerful features you'll encounter is the ability to use libraries. Libraries are collections of pre-built classes and interfaces that you can use to develop your programs more efficiently and effectively. To make use of these libraries, it is essential to understand the import syntax in Java.

What are Libraries in Java?

Java libraries are sets of classes, interfaces and other packages that provide ready-to-use functionality, such as input and output operations, string manipulation, working with networks, developing graphical interfaces, among others. They are designed to help developers avoid rewriting common and often complex code. Java Standard Edition comes with a rich standard library known as Java Standard Library or Java API.

Library Import

To use classes or interfaces from a library in your Java program, you must first import them. The import is done at the beginning of the source code file, before the class declaration, and follows the following syntax:

import package.ClassName;
import package.*;

The first example imports a specific class from the package, while the second imports all classes contained in the specified package. Using the asterisk (*) is known as a generic import or wildcard.

Import Example

Suppose you want to use the ArrayList class, which is part of the java.util package. The import would look like this:

import java.util.ArrayList;

With this line of code at the top of your file, you can now create ArrayList objects in your program without having to reference the full package name every time you use the class.

Why Import?

Importing classes is important for several reasons. First, it improves code readability as you don't have to use long package names every time you reference a class. Second, it facilitates code maintenance, as changes to the packages used only require adjustments to the import declarations. Third, it allows the Java compiler to find the classes your program needs to compile and run correctly.

Static Import

Java also supports static import, which allows you to access static members (fields and methods) of a class directly without needing to qualify with the class name. For example:

import static java.lang.Math.PI;
import static java.lang.Math.sqrt;

After importing PI and sqrt statically, you can directly use PI and call sqrt() in your code without prefixing them with Math.

Name Conflicts and Resolution

Sometimes, a name conflict may occur when two classes with the same name are imported from different packages. To resolve this, you can do one of the following:

  • Avoid generic import and import only the necessary classes.
  • Use the full class name, including the package name, where the conflict occurs.

For example, if you have a conflict with the Date class of java.util and java.sql, you can resolve the conflict as follows:

import java.util.Date;
import java.sql.*;

And then when you reference the Date class, the compiler will assume you are referring to the version of java.util. If you need to use the Date class from java.sql, you must reference it by its full name:

java.sql.Date sqlDate = new java.sql.Date(...);

Good Import Practices

Although it's tempting to use generic imports to save time, it's considered good practice to only import the classes you actually need. This makes the code clearer and avoids unnecessary importing of classes that can increase the size of your program and potentially introduce name conflicts.

Conclusion

Understanding library imports is an essential step to becoming proficient in Java. This allows you to take advantage of the powerful tools that the language and its vast standard library offer, saving time and effort in the software development process. Remember to use importing judiciously to keep your code organized, efficient, and maintainable.

As you advance in learning Java and begin to explore third-party libraries beyond the standard library, you will find even more cases where importing becomes vital. Therefore, practice importing classes and familiarize yourself with the structure of Java libraries. This will be invaluable as you begin to build more complex programs and powerosos.

Now answer the exercise about the content:

_Which of the following statements about importing libraries in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java syntax: Analysis of Access Modifiers (public, private, protected)

Next page of the Free Ebook:

19Basic Java syntax: Analysis of Access Modifiers (public, private, protected)

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text