Static Import in Java

Naveen AnamaneniUpdated · 3 min read
easyJava 25javapackages

Formulas full of Math. get hard to read: Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)). A static import brings static members (fields and methods) of a class into scope, so you can write sqrt(pow(a, 2) + pow(b, 2)) instead. It's a small convenience that reads well in the right places and confuses in the wrong ones.

Writing a Static Import

Geometry.java▶ Try it
import static java.lang.Math.PI;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
 
public class Geometry {
    public static void main(String[] args) {
        double radius = 3;
        System.out.printf("Circle area: %.2f%n", PI * pow(radius, 2));
 
        double a = 6, b = 8;
        System.out.println("Hypotenuse: " + sqrt(pow(a, 2) + pow(b, 2)));
    }
}
Output
Circle area: 28.27
Hypotenuse: 10.0

How it works

  1. import static java.lang.Math.PI; brings in one static field. PI can now be used without Math..
  2. pow and sqrt are imported the same way. Note the word static after import.
  3. import static java.lang.Math.*; would import every static member of Math at once.

Good Uses

UseExample
Math-heavy codesqrt, pow, PI
Constants used oftenimport static java.util.concurrent.TimeUnit.SECONDS;
Test assertionsimport static org.junit.jupiter.api.Assertions.assertEquals; (testing, later in the course)
Factory and builder methodsimport static java.util.stream.Collectors.toList; (streams, later in the course)

In each case the name alone is clear: nobody wonders where assertEquals or sqrt come from.

Ambiguity

If two static imports bring in the same name, using it is a compile error:

Amb2.java
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Long.MAX_VALUE;
 
public class Amb2 {
    long limit = MAX_VALUE;
}
Compiler output
Amb2.java:5: error: reference to MAX_VALUE is ambiguous
    long limit = MAX_VALUE;
                 ^
  both variable MAX_VALUE in Integer and variable MAX_VALUE in Long match
1 error

Write Long.MAX_VALUE instead. And a field or method declared in your own class always shadows a statically imported one with the same name.

Common Mistakes

import static java.lang.Math; A static import names a member (or *), not just a class.

Statically importing everything from several classes. Readers can't tell where names come from, and clashes appear.

Expecting a static import to work for instance methods. Only static members can be statically imported.

Practice

  1. Easy: Rewrite a program that converts Celsius to Fahrenheit and prints the result with Math.round, using a static import for round.
  2. Harder: Compute compound interest, p * pow(1 + r / 100, years), for Rs. 1,00,000 at 7% for 5 years, using static imports for pow and round, and print it rounded to the nearest rupee.

Quick check: Static import

4 questions
1Which line imports only sqrt?
2What can be statically imported?
3Two static imports both bring in MAX_VALUE, and you use it. Result?
4When is a static import a good idea?

Key Takeaways

Was this tutorial helpful?

Lesson 148 of 482