Quiz: Module Import Declarations in Java: import module
Study the lesson first: Module Import Declarations in Java: import module
Total: 6 questions
1. What is import module in Java and how does it differ from a single-type import and a package import?
What is import module in Java and how does it differ from a single-type import and a package import?
import module M; is an import declaration that, in a single line, imports all public classes and interfaces from all packages module M exports. It is a standard feature since Java 25 (JEP 511) and works in any file, both in a modular project and in ordinary class-path code.
- Single-type import,
import java.util.List;: one class or interface. - Type-import-on-demand,
import java.util.*;: all public types of one package. - Module import,
import module java.base;: all public types of all packages the module exports, the widest kind.
The java.base module exports more than fifty packages (java.util, java.io, java.nio.file, java.time, java.math, java.util.function, java.util.stream and others), so List, Path, LocalDate and BigDecimal are available after the single line import module java.base; instead of a dozen package imports.
2. Which types does import module import, and which does it not?
Which types does import module import, and which does it not?
import module M; imports the public top-level types of the packages that module M exports to the current code (plus the packages of the modules M requires transitively).
- Each exported package separately, but all at once:
java.utilandjava.util.concurrentare different packages, butjava.baseexports both, so bothListandThreadLocalRandomare available. - Internal packages are not imported:
jdk.internal.misc.Unsafegivescannot find symbol. This holds even when a module imports itself:import module com.shop.catalog;insidecom.shop.cataloggives no access to its own non-exported package (an ordinaryimportis needed there). - Qualified exports count: a package from
exports P to Mis imported only in the code of moduleM. - Nested types only through the outer class:
Map.Entryworks, plainEntrydoes not. - Static members are not imported:
max(1, 2)does not compile; you needMath.max()orimport static java.lang.Math.*;.
3. How is import module related to requires transitive, and why is List not found after import module java.sql?
How is import module related to requires transitive, and why is List not found after import module java.sql?
Besides the module's own packages, import module M imports the packages of all modules that M requires transitively (requires transitive). An ordinary requires in M does not affect the import.
java.sqlrequiresjava.loggingandjava.xmltransitively, soimport module java.sql;givesConnection,Logger(java.util.logging) andDocumentBuilderFactory(javax.xml.parsers).Listis not found (cannot find symbol): every module readsjava.base, butjava.sqldoes not require it transitively. Addimport module java.base;orimport java.util.List;.- The aggregator
java.serequiresjava.base,java.sql,java.desktopand the other Java SE modules transitively, soimport module java.se;imports everything at once, together with the name clashes.
4. When does import module cause a reference is ambiguous error and how do you fix it?
When does import module cause a reference is ambiguous error and how do you fix it?
If two imported modules contain types with the same simple name, the reference to X is ambiguous error appears only when that name is used. Overlapping module imports are not an error by themselves. Typical pairs: java.util.List (java.base) and java.awt.List (java.desktop), java.util.Date (java.base) and java.sql.Date (java.sql).
A clash is resolved with a narrower import, because the kinds of imports have different precedence:
- a class declared in this file or package (highest);
- a single-type import,
import java.util.List;; - a type-import-on-demand, including the implicit
java.lang.*,import java.util.*;; - a module import (lowest).
You can also write the fully qualified class name. The order of import lines does not matter: with import module java.base; and import java.awt.*; the name List means java.awt.List. A compact source file implicitly has import module java.base;, so a single line import module java.sql; already makes Date ambiguous; the fix is the same, import java.sql.Date;.
5. How does import module differ in a named module and in code without module-info?
How does import module differ in a named module and in code without module-info?
import module does not replace requires: it only shortens type imports, while access to a module is still defined by module-info.java.
- A named module can import only a module it reads. Without
requires com.shop.catalog;the lineimport module com.shop.catalog;givesmodule com.shop.app does not read: com.shop.catalog; the same goes for JDK modules,does not read: java.sql. Withrequiresthe import follows all visibility rules: exported packages, qualified exports,requires transitive. - Class-path code lives in the unnamed module, which reads the JDK modules without
requires:import module java.sql;andimport module java.net.http;compile right away, nomodule-info.javaneeded. - The exception is
java.se: the unnamed module does not read it by default, andimport module java.se;givesunnamed module does not read: java.se. It compiles withjavac --add-modules java.se.
6. Since which Java version is import module available, where do you write it and what are the most common errors?
Since which Java version is import module available, where do you write it and what are the most common errors?
History: Java 23, preview (JEP 476); Java 24, second preview (JEP 494: on-demand imports now beat module imports, java.se now requires java.base transitively); Java 25, standard feature (JEP 511), no --enable-preview needed. With --release 24 or lower the compiler reports module imports are not supported in -source 24.
import module goes where ordinary imports go: after package and before the class declaration, mixed with them in any order. Typical errors:
import module java.util;givesimported module not found: java.util: that is a package name, not a module;import module com.example.nothing;givesimported module not found;import module java.*;: wildcards are not supported;import static module java.base;: there is no static module import.
When to use it: learning examples, scripts, compact source files, prototypes, JShell and code that heavily uses one library module. In production code of a large project, carefully: explicit import lines protect you from clashes when a new library version adds a class with the same name. import module doesn't affect program speed: bytecode always uses fully qualified class names.