Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.
Although they don’t affect the runtime behavior of the application after compilation, removing them will:
Imports for types mentioned in Javadocs are ignored.
While it’s not difficult to remove these unneeded lines manually, modern code editors support the removal of every unnecessary import with a single click from every file of the project.
package myapp.helpers;
import module java.logging; // Module imports are a Java 25 feature
import module java.logging; // Noncompliant - module is imported twice
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.*; // Noncompliant - package is imported twice
import java.lang.Runnable; // Noncompliant - java.lang is imported by default
public class FileHelper {
private static final Logger LOGGER = Logger.getLogger("FileLogger");
public static String readFirstLine(String filePath) throws IOException {
LOGGER.log(Level.INFO, "Reading first line from: " + filePath);
return Files.readAllLines(Paths.get(filePath)).get(0);
}
}
package myapp.helpers;
import module java.logging;
import java.io.IOException;
import java.nio.file.*;
public class FileHelper {
private static final Logger LOGGER = Logger.getLogger("FileLogger");
public static String readFirstLine(String filePath) throws IOException {
LOGGER.log(Level.INFO, "Reading first line from: " + filePath);
return Files.readAllLines(Paths.get(filePath)).get(0);
}
}