By WebNest Studio Engineering Team
Java - Core Tutorial
Static Import
A static import lets you use a class's static members — methods and constants — without prefixing them with the class name every time. Instead of writing Math.sqrt(x) and Math.PI throughout a file, a static import lets you write just sqrt(x) and PI.
Why it matters
A static import lets you write sqrt(x) instead of Math.sqrt(x). It shortens code that uses the same static members repeatedly, which is why test code is full of statically imported assertions. Overused, it hides where a name comes from, so it is a readability decision more than a technical one.
Static Import Syntax
The syntax is import static packageName.ClassName.memberName; for a single member, or import static packageName.ClassName.*; to import all static members of that class. Static imports must appear alongside other import statements, after any package declaration.
When It Helps
Static imports genuinely improve readability in mathematically dense code (using Math members heavily) or when writing unit tests with a library like JUnit, where methods like assertEquals and assertTrue read cleanly without a class prefix repeated on every line.
When It Hurts
Overusing static imports, especially importing * from several classes, makes it hard to tell at a glance which class a given method or constant actually comes from — readers lose an important visual cue. Most style guides recommend reserving static imports for a small number of extremely well-known members (like Math constants or a testing framework's assertions), and avoiding them when the source class is not immediately obvious from context.
Example
Using static import to drop the Math class prefix
import static java.lang.Math.PI;
import static java.lang.Math.pow;
public class StaticImportDemo {
public static void main(String[] args) {
double radius = 3.0;
double area = PI * pow(radius, 2); // no "Math." prefix needed
System.out.println(area);
}
}
28.274333882308138
Common Mistakes
- Overusing wildcard static imports from multiple classes, which makes it unclear where a method or constant actually comes from.
- Statically importing members whose names collide with other identifiers in the file, causing confusing compile errors.
- Assuming static import lets you import instance methods — it only works for static members.
- Forgetting static import is purely a readability feature; it does not change behavior, performance, or accessibility rules.
Exercise
Rewrite this program with static imports so that sqrt and max are called without the Math. prefix. The output must stay the same.
Expected output: 4.0
public class StaticImportPractice {
public static void main(String[] args) {
double result = Math.sqrt(Math.max(16, 9));
System.out.println(result);
}
}
Show hints
- A static import names the class and the member:
import static java.lang.Math.sqrt; - Import statements go above the class, after any package declaration.
Show solution
import static java.lang.Math.max;
import static java.lang.Math.sqrt;
public class StaticImportPractice {
public static void main(String[] args) {
double result = sqrt(max(16, 9));
System.out.println(result); // 4.0
}
}
Quick quiz
1. Which statement correctly imports the constant PI so it can be used as PI?
- A. import java.lang.Math.PI;
- B. static import java.lang.Math.PI;
- C. import static java.lang.Math.PI;
- D. import Math.PI static;
Show answer
C. The keywords appear in the order import static, followed by the fully qualified class and member name.
2. What can a static import bring into scope?
- A. Any class
- B. Constructors
- C. Instance methods
- D. Only static fields and static methods
Show answer
D. A static import applies to static members of a class or interface. Instance members always need an object.
3. Two static imports bring in methods with the same name and parameters from different classes, and you call that name. What happens?
- A. The first import wins
- B. It does not compile because the call is ambiguous
- C. The last import wins
- D. Both methods are called
Show answer
B. The compiler cannot decide which method you mean, so the call is rejected until you qualify it with the class name.
Interview questions
What is the difference between import and import static?
Show answer
A normal import lets you refer to a class by its simple name, such as List instead of java.util.List. An import static lets you refer to a static member of a class without the class name, such as PI instead of Math.PI.
When should you avoid static imports?
Show answer
When they make it unclear where a name comes from. Wildcard static imports from several classes, or importing generic names such as of or valueOf, force the reader to hunt for the source. They suit a small number of well-known members used many times, such as constants or test assertions.
Key Points to Remember
- Syntax: import static package.Class.member; or import static package.Class.*; for all static members.
- It only applies to static members — fields and methods marked static.
- It can improve readability for heavily used utility members like Math constants or test assertions.
- Overuse harms readability by hiding which class a member actually belongs to.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering