Week 148 — What are module import declarations and how are they used?

Question of the Week #148
What are module import declarations and how are they used?
6 Replies
MrMisha | Coder
MrMisha | Coder2mo ago
When referencing a class from a different package in a Java program, the fully qualified name of that class must either be written out or class must be imported. This is typically done using the import keyword followed by the fully qualified name of the imported class:
import java.time.LocalDate;

class SomeClass {
void main() {
IO.println("The current date is " + java.time.LocalDate.now());//using the fully qualified name when referencing the class does not need importing
IO.println("The current date is " + LocalDate.now());//since LocalDate is imported, the class name can be used as well
}
}
import java.time.LocalDate;

class SomeClass {
void main() {
IO.println("The current date is " + java.time.LocalDate.now());//using the fully qualified name when referencing the class does not need importing
IO.println("The current date is " + LocalDate.now());//since LocalDate is imported, the class name can be used as well
}
}
In addition to importing classes, it is also possible to use wildcard imports that include all classes in a package. Wildcard imports are written with a * instead of the class name:
import java.time.*;

class SomeClass {
void main() {
// all classes from the java.time package can be used with their (simple) class name
IO.println("The current date is " + LocalDate.now());
IO.println("The current time is " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute());

// classes in other packages (e.g. java.time.format) cannot be referenced that way without an additional import
// IO.println("The current time is " + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")));
}
}
import java.time.*;

class SomeClass {
void main() {
// all classes from the java.time package can be used with their (simple) class name
IO.println("The current date is " + LocalDate.now());
IO.println("The current time is " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute());

// classes in other packages (e.g. java.time.format) cannot be referenced that way without an additional import
// IO.println("The current time is " + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")));
}
}
However, this is not the end. It is not just possible to import all classes in a package with a single import statement but it is also possible to do that for an entire (JPMS) module using import module followed by the name of the module to import. For instance, both the java.time and java.util packages are in the java.base module which can be imported using import module java.base;
import module java.base;

class SomeClass {
void main() {
IO.println("The current date is " + LocalDate.now()); // LocalDate is in the java.base module

List<String> stringList = List.of("Hello", "World");//this also works for other classes in java.base
}
}
import module java.base;

class SomeClass {
void main() {
IO.println("The current date is " + LocalDate.now()); // LocalDate is in the java.base module

List<String> stringList = List.of("Hello", "World");//this also works for other classes in java.base
}
}
When using compact source files, the java.base module is imported implicitly so classes within that module don't have to be imported explicitly:
// no imports necessary

void main() {
IO.println("The current date is " + LocalDate.now());
IO.println(List.of("Hello", "World"));
}
// no imports necessary

void main() {
IO.println("The current date is " + LocalDate.now());
IO.println(List.of("Hello", "World"));
}
MrMisha | Coder
MrMisha | Coder2mo ago
If two types with the same name are imported via module imports, references to it are ambiguous. For example, the packages java.text, java.lang.annotation and java.lang.classfile (all in the java.base module) contain a type named Annotation.
import module java.base;

class SomeClass {
void main() {
//IO.println(Annotation.class);// compilation error because Annotation is ambiguous
}
}
import module java.base;

class SomeClass {
void main() {
//IO.println(Annotation.class);// compilation error because Annotation is ambiguous
}
}
To still use such types when module imports are used, the developer needs to decide which of these types to use and import the chosen type explicitly:
import module java.base;
import java.lang.annotation.Annotation;

class SomeClass {
void main() {
IO.println(Annotation.class);// interface java.lang.annotation.Annotation
}
}
import module java.base;
import java.lang.annotation.Annotation;

class SomeClass {
void main() {
IO.println(Annotation.class);// interface java.lang.annotation.Annotation
}
}
This can also occur with types from different modules when both modules are imported using an import module statement.
import module java.base;
import module java.sql;

import java.lang.reflect.Array;// vs java.sql.Array
import java.sql.Date;//vs java.util.Date

class SomeClass {
void main() {
IO.println(Array.class);//class java.lang.reflect.Array
IO.println(Date.class);//class java.sql.Date
}
}
import module java.base;
import module java.sql;

import java.lang.reflect.Array;// vs java.sql.Array
import java.sql.Date;//vs java.util.Date

class SomeClass {
void main() {
IO.println(Array.class);//class java.lang.reflect.Array
IO.println(Date.class);//class java.sql.Date
}
}
📖 Sample answer from dan1st
MrMisha | Coder
MrMisha | Coder2mo ago
Module import declarations are statements that allow for the concise import of an entire module's exported packages and their public classes and interfaces.
Submission from kasinathan2008
MrMisha | Coder
MrMisha | Coder2mo ago
I can use module import except import lot of source file for example import module java.base; it will help people import java source except writing import java.util.ArryList; import java.util.List; import java.lang; etc. what if module have two same name? java.base and java.sql both have Date type It is need to add a import statement import module java.base; import module java.sql; import java.sql.Date; or
MrMisha | Coder
MrMisha | Coder2mo ago
java.sql.Data date; instead of Date date;
Submission from cotafn_30465
MrMisha | Coder
MrMisha | Coder2mo ago
Java code can be organized with: methods, classes, packages and modules. Classes from other packages can be imported like so:
import java.util.List;
import java.util.ArrayList;
...
List<String> list = new ArrayList<>();
import java.util.List;
import java.util.ArrayList;
...
List<String> list = new ArrayList<>();
But as the program grows, it can lead to very lengthy import statements which are typically resolved by using wildcards:
import java.util.*;
import java.io.*
...
List<String> list = new ArrayList<>();
try {
Scanner sc = new Scanner(new FileInputStream("example.txt"));
while (sc.hasNextLine()) {
list.add(sc.nextLine());
}
} catch (FileNotFoundException e) {
System.exit(1);
}
import java.util.*;
import java.io.*
...
List<String> list = new ArrayList<>();
try {
Scanner sc = new Scanner(new FileInputStream("example.txt"));
while (sc.hasNextLine()) {
list.add(sc.nextLine());
}
} catch (FileNotFoundException e) {
System.exit(1);
}
Module import is a new Java 25 feature that allows you to import all the packages that a module provides in a single statement.
import module java.base;
import module java.base;
This was added as a convenience for when you don't want to write verbose import statements, for example: when trying new things.
⭐ Submission from giinko0103

Did you find this page helpful?