This rule raises an issue when module imports are present but not placed first, or when regular imports and static imports are not properly grouped together in files that contain module imports.
With the introduction of module imports in Java 25 (JEP 511), it’s important to organize import declarations to improve readability and make shadowing behavior explicit when module imports are used. Module imports should come first as they are the least specific and can be shadowed by more specific package and type imports.
When a file contains module imports, regular imports and static imports should each be grouped together, though the relative order within and between these groups is flexible. This allows code to follow various established style guides (such as the Google Java Style Guide) while still maintaining basic organization that makes import shadowing behavior clear.
| Note | This rule only applies to files that contain at least one module import declaration. Files without module imports are not affected by this rule. |
When your code contains module imports, organize import declarations with the following requirements:
import module java.base;)import javax.swing.text.*; and specific like
import java.util.List;)import static java.util.Collections.*; and specific
like import static java.util.regex.Pattern.compile;)The relative order between regular and static import groups is flexible, as is the ordering within each group. This allows compliance with various style guides while maintaining clear organization.
If your code does not use module imports, this rule does not apply.
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 must come first
import module java.base;
import module java.desktop;
// Regular imports grouped together
import java.sql.Date;
import javax.swing.text.*;
import java.util.List;
// Static imports grouped together
import static java.util.Collections.*;
import static java.util.regex.Pattern.compile;
class Foo {
// ...
}