CKTIS / Java
import — names from other packages
import — names from other packages: An import declaration in a compilation unit makes a type or static member available by simple name; it neither loads a class at runtime nor creates an alias.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
importAn import declaration in a compilation unit makes a type or static member available by simple name; it neither loads a class at runtime nor creates an alias.
Use single-type imports for clarity, static imports sparingly for recognizable constants or functions, and a qualified name to resolve a conflict.
An on-demand star import does not include subpackages; two accessible declarations with the same simple name can cause an ambiguity error.
java.lang and current-package types need no import; imports follow package and precede types, and an unused import does not change execution.
The single-type import exposes List and the static import exposes joining; the collector joins a and b, so stdout is exactly a-b.
Examples
import java.util.List;
import static java.util.stream.Collectors.joining;
public class Main {
public static void main(String[] args) {
String joined = List.of("a", "b").stream().collect(joining("-"));
System.out.println(joined);
}
}
Result
a-b
The single-type import exposes List and the static import exposes joining; the collector joins a and b, so stdout is exactly a-b.