Sub topic 9
Static Imports
Static imports let you use a static field or method without writing the class name each time. For example, Math.max(3, 7) can become max(3, 7) when you import max statically.
Static imports do not copy a member into your class. They only provide a shorter name for a member that still belongs to its original class.
Importing One Static Member
Use import static, followed by the class name, a dot, and the member name:
import static java.lang.Math.max;
public class ScoreDemo {
public static void main(String[] args) {
int higherScore = max(82, 91);
System.out.println(higherScore);
}
}max remains a method of java.lang.Math. The import only allows the call without the Math. prefix.
The same syntax works for static fields:
import static java.lang.Math.PI;
public class CircleDemo {
public static void main(String[] args) {
double radius = 2.0;
double circumference = 2 * PI * radius;
System.out.println(circumference);
}
}Java automatically imports types from java.lang, including Math, but it does not automatically make their static members available by short name. You still need a static import for PI or max.
Importing Several Members
You can write one static import for each member:
import static java.lang.Math.max;
import static java.lang.Math.min;
public class RangeDemo {
public static void main(String[] args) {
int low = min(12, 5);
int high = max(12, 5);
System.out.println(low);
System.out.println(high);
}
}You can also use a wildcard to import all accessible static members from a class:
import static java.lang.Math.*;
public class MathDemo {
public static void main(String[] args) {
System.out.println(abs(-8));
System.out.println(sqrt(25));
}
}A wildcard does not import members from every class. It applies only to the named class, Math in this example.
Choosing Between Explicit and Wildcard Imports
Explicit imports show exactly which members a file uses. They make the source easier to read and help prevent name conflicts. Wildcard imports are shorter, but they can hide where a name came from.
| Form | Example | Typical use |
|---|---|---|
| Explicit static import | `import static java.lang.Math.PI;` | A few well-known members |
| Wildcard static import | `import static java.lang.Math.*;` | Many members from one class |
A static import can become confusing when the short name is common. For example, several libraries might define a method named of, get, or isEmpty. If two static imports provide the same simple name, an unqualified call can become ambiguous and fail to compile.
You can avoid the problem by removing one import and using the class name at the call site:
import static java.lang.Math.max;
public class ClearDemo {
public static void main(String[] args) {
System.out.println(max(4, 9));
System.out.println(Math.random());
}
}The explicit Math.random() makes the source clear even though max uses a static import.
How Name Resolution Works
The compiler first checks names declared in the current class and its enclosing scopes. It then considers imported static members. A local declaration can therefore hide an imported name.
Static imports do not change inheritance, access control, or overriding. The member must still be accessible, and a static method is still selected by its class rather than by an object's runtime type.
Exercise
Exercise
Rewrite Math.max(10, 15) using a static import for max. When should you prefer the qualified form instead?
Show answer
Add import static java.lang.Math.max; and write max(10, 15). Prefer Math.max(10, 15) when the class name makes the code clearer or avoids a naming conflict.