Sub topic 10
Static Imports in Java
Assume you can write and run a Java class, understand packages and ordinary imports, and recognize the difference between static and instance members. A static import lets you use a static field, method, or nested type without writing its class name each time.
Why use a static import?
Normally, code calls a static method through its declaring class:
int larger = Math.max(12, 19);
double circleArea = Math.PI * radius * radius;A static import makes the member available by its simple name:
import static java.lang.Math.PI;
import static java.lang.Math.max;The code can then write:
int larger = max(12, 19);
double circleArea = PI * radius * radius;The import changes how the source code names the member. It does not copy the method or field into your class.
Importing individual members
Use this form for one static member:
import static package.ClassName.memberName;The following program imports the max method and PI constant separately:
import static java.lang.Math.PI;
import static java.lang.Math.max;
public class StaticImportDemo {
public static void main(String[] args) {
int larger = max(3, 7);
double circumference = 2 * PI * 5;
System.out.println("Larger value: " + larger);
System.out.println("Circumference: " + circumference);
}
}Math belongs to java.lang, which Java imports implicitly. The static keyword in the import is still required because this import targets static members rather than the Math class itself.
Static imports can target public static fields, methods, and nested types. They cannot target instance members, because an instance member requires a particular object.
Importing all static members
An on-demand static import uses .*:
import static java.lang.Math.*;This allows names such as PI, sqrt, and round without the Math. prefix.
| Import | Example use | Effect |
|---|---|---|
| `import static java.lang.Math.PI;` | `PI` | Imports one member |
| `import static java.lang.Math.max;` | `max(2, 5)` | Imports one method |
| `import static java.lang.Math.*;` | `sqrt(16)` | Makes all accessible static members available on demand |
Wildcard imports do not import members from unrelated classes. They also do not automatically cause conflicts at the import line. A conflict becomes important when the same simple name could refer to more than one member.
Readability and name conflicts
Static imports can make small calculations and test assertions easier to read. They can also hide where a name comes from. This is especially confusing when several classes define methods with the same name, such as of, format, or parse.
Prefer a single-member static import when it clearly improves the code. Avoid broad wildcard static imports in shared or unfamiliar code.
If two static members have the same simple name, qualify one call with its class name:
import static java.lang.Math.max;
public class ConflictExample {
public static void main(String[] args) {
int result = max(4, 9);
System.out.println(result);
}
}You can also avoid a static import entirely and write Math.max(4, 9). A class-qualified name is often the clearest choice when the origin matters.
Exercise
Rewrite Math.min(8, 3) using a single static import, and state one reason to keep the Math. prefix instead.
Show answer
Use import static java.lang.Math.min; and call min(8, 3); keep Math. when showing the method's origin improves clarity.