concept import statement in category java

appears as: import statements, import statement, The import statement, n import statement, An import statement, import statement, import statements
Java SE 11 Programmer I Certification Guide

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide.

Exam objectives covered in this chapter

What you need to know

[2.1] Create executable Java applications with a main method.

The right method signature for the main method to create an executable Java application.

The arguments that are passed to the main method.

[2.2] Compile and run a Java program from the command line.

[2.3] Import other Java packages to make them accessible in your code.

Compiling and executing a Java program using javac and java tools.

Understand packages and import statements. Get the right syntax and semantics to import classes and interfaces from packages in your own classes.

Class Cubicle can refer to class LivingRoom without using an import statement:

package office;
class Cubicle {
    home.LivingRoom livingRoom;                    #A
}

Class Cubicle can use the simple name for class LivingRoom by using the import statement:

package office;
import home.LivingRoom;               #A
class Cubicle {
    LivingRoom livingRoom;            #B
}

Note The import statement doesn’t embed the contents of the imported class in your class, which means that importing more classes doesn’t increase the size of your own class. It lets you use the simple name for a class or interface defined in a separate package.

Note that the import statement follows the package statement but precedes the class declaration. What happens if the class AnnualExam isn’t defined in a package? Will there be any change in the code if the classes AnnualExam and ExamQuestion are related, as depicted in figure 2.9?

OCA Java SE 8 Programmer I Certification Guide

This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

Classes and interfaces in the same package can use each other without prefixing their names with the package name. But to use a class or an interface from another package, you must use its fully qualified name, that is, packageName.anySubpackageName.ClassName. For example, the fully qualified name of class String is java.lang.String. Because using fully qualified names can be tedious and can make your code difficult to read, you can use the import statement to use the simple name of a class or interface in your code.

Figure 1.16. LivingRoom can be accessed in Cubicle by using its fully qualified name. It can also be accessed using its simple name if you also use the import statement.