Quiz: 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?

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?

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.util and java.util.concurrent are different packages, but java.base exports both, so both List and ThreadLocalRandom are available.
  • Internal packages are not imported: jdk.internal.misc.Unsafe gives cannot find symbol. This holds even when a module imports itself: import module com.shop.catalog; inside com.shop.catalog gives no access to its own non-exported package (an ordinary import is needed there).
  • Qualified exports count: a package from exports P to M is imported only in the code of module M.
  • Nested types only through the outer class: Map.Entry works, plain Entry does not.
  • Static members are not imported: max(1, 2) does not compile; you need Math.max() or import static java.lang.Math.*;.
3. 

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.sql requires java.logging and java.xml transitively, so import module java.sql; gives Connection, Logger (java.util.logging) and DocumentBuilderFactory (javax.xml.parsers).
  • List is not found (cannot find symbol): every module reads java.base, but java.sql does not require it transitively. Add import module java.base; or import java.util.List;.
  • The aggregator java.se requires java.base, java.sql, java.desktop and the other Java SE modules transitively, so import 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?

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:

  1. a class declared in this file or package (highest);
  2. a single-type import, import java.util.List;;
  3. a type-import-on-demand, including the implicit java.lang.*, import java.util.*;;
  4. 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?

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 line import module com.shop.catalog; gives module com.shop.app does not read: com.shop.catalog; the same goes for JDK modules, does not read: java.sql. With requires the 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; and import module java.net.http; compile right away, no module-info.java needed.
  • The exception is java.se: the unnamed module does not read it by default, and import module java.se; gives unnamed module does not read: java.se. It compiles with javac --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?

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; gives imported module not found: java.util: that is a package name, not a module;
  • import module com.example.nothing; gives imported 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.

Page 1 of 1