DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 2 — Static Import in Java

Introduced in Java 5, the static import feature lets you access static members of a class without using the class name.

It can make code shorter, but if overused, it can also make code harder to read.

In this article, you'll learn:

  • What static import is
  • Why it was introduced
  • How it works
  • When to use it
  • Common interview questions
  • Best practices

What Is Static Import?

Normally, static members are accessed using the class name.

Example

System.out.println(Math.sqrt(25));
System.out.println(Math.max(10, 20));
Enter fullscreen mode Exit fullscreen mode

Here,

  • Math is the class.
  • sqrt() and max() are static methods.

Since they are static, Java allows us to import them directly.


Why Was Static Import Introduced?

Before Java 5, every static member had to be accessed using its class name.

System.out.println(Math.sqrt(16));
System.out.println(Math.random());
System.out.println(Math.max(100, 200));
Enter fullscreen mode Exit fullscreen mode

Java 5 introduced static import to reduce repetition and make code shorter.


Syntax

To import a single static member:

import static java.lang.Math.sqrt;
Enter fullscreen mode Exit fullscreen mode

To import all static members:

import static java.lang.Math.*;
Enter fullscreen mode Exit fullscreen mode

Accessing Static Members Without Static Import

public class Demo {

    public static void main(String[] args) {

        System.out.println(Math.sqrt(25));
        System.out.println(Math.max(15, 30));
        System.out.println(Math.random());

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

5.0
30
0.748291...
Enter fullscreen mode Exit fullscreen mode

Using Static Import

import static java.lang.Math.*;

public class Demo {

    public static void main(String[] args) {

        System.out.println(sqrt(25));
        System.out.println(max(15, 30));
        System.out.println(random());

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

5.0
30
0.748291...
Enter fullscreen mode Exit fullscreen mode

Notice that the Math class name is no longer required.


Importing Only One Static Member

Instead of importing everything, you can import just one member.

import static java.lang.Math.sqrt;
Enter fullscreen mode Exit fullscreen mode

Example

import static java.lang.Math.sqrt;

public class Demo {

    public static void main(String[] args) {

        System.out.println(sqrt(49));

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

7.0
Enter fullscreen mode Exit fullscreen mode

Only sqrt() is available directly.

Calling

max(10, 20);
Enter fullscreen mode Exit fullscreen mode

would produce a compile-time error because it wasn't imported.


Wildcard Static Import

Imports every public static member.

import static java.lang.Math.*;
Enter fullscreen mode Exit fullscreen mode

Now you can directly call:

sqrt()
max()
min()
random()
abs()
pow()
PI
E
Enter fullscreen mode Exit fullscreen mode

without writing Math. every time.


Static Import with Variables

Static import works for variables as well.

Example

import static java.lang.Math.PI;

public class Demo {

    public static void main(String[] args) {

        System.out.println(PI);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

3.141592653589793
Enter fullscreen mode Exit fullscreen mode

Understanding System.out.println()

One of the most frequently asked Java interview questions is:

What exactly is System.out.println()?

Let's break it down.

System.out.println("Hello, Rajesh!");
Enter fullscreen mode Exit fullscreen mode
  • System → Class in the java.lang package
  • out → Static variable of type PrintStream
  • println() → Method of the PrintStream class

Visual representation

System
   │
   ▼
out (PrintStream object)
   │
   ▼
println()
Enter fullscreen mode Exit fullscreen mode

Using Static Import with System.out

Instead of writing

System.out.println("Hello");
Enter fullscreen mode Exit fullscreen mode

we can statically import out.

import static java.lang.System.out;

public class Demo {

    public static void main(String[] args) {

        out.println("Hello");
        out.println("Welcome, Rajesh!");

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

Hello
Welcome, Rajesh!
Enter fullscreen mode Exit fullscreen mode

Static Import Resolution Order

When Java resolves static members, it follows this order:

  1. Static members declared in the current class
  2. Explicit static imports
  3. Wildcard static imports

Example

import static java.lang.Math.*;

public class Demo {

    static int max(int a, int b) {
        return 999;
    }

    public static void main(String[] args) {

        System.out.println(max(10, 20));

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

999
Enter fullscreen mode Exit fullscreen mode

The compiler first checks the current class before looking at imported static members.


Ambiguity with Static Imports

Suppose two classes expose static members with the same name.

Example

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.MAX_VALUE;
Enter fullscreen mode Exit fullscreen mode

Now,

System.out.println(MAX_VALUE);
Enter fullscreen mode Exit fullscreen mode

Compile-time error

reference to MAX_VALUE is ambiguous
Enter fullscreen mode Exit fullscreen mode

The compiler cannot determine which MAX_VALUE you intended.


General Import vs Static Import

General Import Static Import
Imports classes or interfaces Imports static fields and methods
Lets you use class names directly Lets you use static members directly
Example: import java.util.ArrayList; Example: import static java.lang.Math.sqrt;

When Should You Use Static Import?

Static import is useful when working heavily with utility classes.

Examples include:

  • Math
  • Assertions in JUnit
  • Constants
  • Utility methods

Example

assertEquals(expected, actual);
Enter fullscreen mode Exit fullscreen mode

instead of

Assertions.assertEquals(expected, actual);
Enter fullscreen mode Exit fullscreen mode

JUnit projects commonly use static imports for better readability.


When Should You Avoid Static Import?

Avoid static imports when they make code less clear.

Instead of

max(a, b)
Enter fullscreen mode Exit fullscreen mode

many developers prefer

Math.max(a, b)
Enter fullscreen mode Exit fullscreen mode

because it's immediately obvious where the method comes from.

Large projects generally use static imports sparingly.


Common Beginner Mistakes

Importing Everything Unnecessarily

import static java.lang.Math.*;
Enter fullscreen mode Exit fullscreen mode

when you only need

sqrt()
Enter fullscreen mode Exit fullscreen mode

Prefer importing only what you use.


Forgetting That Static Import Works Only for Static Members

This is invalid.

import static java.util.ArrayList.add;
Enter fullscreen mode Exit fullscreen mode

because add() is not a static method.


Creating Ambiguous Imports

Importing static members with identical names from multiple classes causes ambiguity.


Interview Questions

Is static import available in all Java versions?

No.

It was introduced in Java 5.


Can we static import instance methods?

No.

Only static members can be imported.


Can we static import variables?

Yes.

Example

import static java.lang.Math.PI;
Enter fullscreen mode Exit fullscreen mode

Is static import mandatory?

No.

It's purely optional.


Does static import improve performance?

No.

It only affects code readability.

There is no runtime performance benefit.


Best Practices

  • Use static import only when it improves readability.
  • Avoid wildcard static imports in large projects.
  • Don't create ambiguous imports.
  • Prefer explicit static imports when importing only a few members.
  • Use static imports commonly in testing frameworks like JUnit.

Quick Memory Trick 🧠

Remember:

General import brings classes. Static import brings static members.

Or even shorter:

  • import → Class
  • static import → Class Members

Key Takeaways

  • Static import was introduced in Java 5.
  • It allows direct access to static methods and variables.
  • Only static members can be imported.
  • Static import improves readability in some situations but can reduce clarity if overused.
  • The compiler resolves current class members before imported static members.
  • Ambiguous static imports result in compile-time errors.
  • Static import is widely used in JUnit but should be used carefully in application code.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)