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.

Why is this an issue?

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.

How to fix it

When your code contains module imports, organize import declarations with the following requirements:

  1. Module imports must come first (e.g., import module java.base;)
  2. Regular imports should be grouped together (both wildcard like import javax.swing.text.*; and specific like import java.util.List;)
  3. Static imports should be grouped together (both wildcard like 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.

Code examples

Noncompliant code example

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 {
    // ...
}

Compliant solution

// 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 {
    // ...
}

Resources

Documentation