Java Language Import statements


Example

// First java/javax packages
import java.util.ArrayList;
import javax.tools.JavaCompiler;

// Then third party libraries
import com.fasterxml.jackson.annotation.JsonProperty;

// Then project imports
import com.example.my.package.ClassA;
import com.example.my.package.ClassB;

// Then static imports (in the same order as above)
import static java.util.stream.Collectors.toList;
  • Import statements should be sorted…

    • …primarily by non-static / static with non-static imports first.
    • …secondarily by package origin according to the following order
      • java packages
      • javax packages
      • external packages (e.g. org.xml)
      • internal packages (e.g. com.sun)
    • …tertiary by package and class identifier in lexicographical order
  • Import statements should not be line wrapped, regardless of whether it exceeds the recommended maximum length of a line.

  • No unused imports should be present.

Wildcard imports

  • Wildcard imports should in general not be used.
  • When importing a large number of closely-related classes (such as implementing a visitor over a tree with dozens of distinct “node” classes), a wildcard import may be used.
  • In any case, no more than one wildcard import per file should be used.