Static import is a Java feature that allows you to access static members (fields and methods) of a class directly without using the class name. It helps reduce repetitive class references and makes code cleaner and more readable.
static keyword in import statementStatic import allows direct access to constants or utility methods such as Math.sqrt() without prefixing the class name.
// Standard import without static import
double result = Math.sqrt(25);
// Static import of Math.sqrt method
import static java.lang.Math.sqrt;
double result = sqrt(25);
// Static import of all Math class members
import static java.lang.Math.*;
public class StaticImportDemo {
public static void main(String[] args) {
double value = sqrt(16);
double power = pow(2, 3);
System.out.println(value);
System.out.println(power);
}
}
4.0
8.0
The methods sqrt() and pow() are called directly without using the Math class name because of static import.
See how code changes when you apply static imports to a Geometry class.
public class Geometry {
public double circleArea(double r) {
// Repeated usage of "Math."
return Math.PI * Math.pow(r, 2);
}
}
PIMath.PI directly