This rule raises an issue when import declarations are not organized into distinct groups based on their kind and specificity level.
Import declarations should be organized into distinct groups based on their kind to improve readability and make shadowing behavior explicit. The grouping order should reflect specificity levels: module imports first (least specific), followed by on-demand package imports (intermediate specificity), single-type imports (most specific), then static on-demand imports, and finally single-static imports.
When imports are mixed or ordered arbitrarily, it becomes harder to understand which declarations might shadow others. Java’s import shadowing rules mean that more specific imports can override less specific ones, so organizing imports by specificity makes these relationships clearer and improves code maintainability by providing a consistent, predictable structure.
Organize import declarations into the following groups, in this order:
import module java.base;) import javax.swing.text.*;) import java.util.List;) import static java.util.Collections.*;) import static java.util.regex.Pattern.compile;) Separate each group with a blank line for better visual organization.
import java.sql.Date;
import module java.base;
import static java.util.Collections.*;
import javax.swing.text.*;
import module java.desktop;
import static java.util.regex.Pattern.compile;
import java.util.List;
class Foo {
// ...
}
// Module imports
import module java.base;
import module java.desktop;
// On-demand package imports
import javax.swing.text.*; // resolves the ambiguity of the simple name Element
// Single-type imports
import java.sql.Date; // resolves the ambiguity of the simple name Date
import java.util.List;
// Static on-demand imports
import static java.util.Collections.*;
// Single-static imports
import static java.util.regex.Pattern.compile;
class Foo {
// ...
}