Programming
Java
Static Modifier
Import Statement
Coding Concepts

What does the static modifier after import mean?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, import static means "bring a static member into scope so I can use it without writing the class name every time." It does not import the class itself in a special way. It imports static fields or static methods from that class.

This is called a static import. It is mostly a readability feature, useful when the same constant or utility method is used repeatedly.

What Changes With a Static Import

Without static import, you call a static member through its class:

java
double radius = 3.0;
double area = Math.PI * Math.pow(radius, 2);
System.out.println(area);

With static import:

java
1import static java.lang.Math.PI;
2import static java.lang.Math.pow;
3
4double radius = 3.0;
5double area = PI * pow(radius, 2);
6System.out.println(area);

The behavior is the same. Only the syntax at the call site changes.

What You Are Allowed to Import

You can statically import:

  • static methods
  • static fields
  • enum constants

Examples:

java
import static java.lang.Math.max;
import static java.lang.Integer.MAX_VALUE;
import static java.time.DayOfWeek.MONDAY;

Then use them directly:

java
int bigger = max(5, 9);
System.out.println(MAX_VALUE);
System.out.println(MONDAY);

Why It Exists

Static import reduces repetition when a member is used heavily. The classic examples are:

  • math functions
  • constants
  • assertion methods in tests

JUnit code is often clearer with static import:

java
1import static org.junit.jupiter.api.Assertions.assertEquals;
2
3import org.junit.jupiter.api.Test;
4
5class PriceTest {
6    @Test
7    void totalIsCalculated() {
8        assertEquals(15, 10 + 5);
9    }
10}

In test code, assertEquals reads naturally enough that the missing class name is usually a benefit.

Wildcard Static Imports

You can also import all static members from a class:

java
import static java.lang.Math.*;

Then use:

java
double value = sqrt(16) + abs(-3);

This is legal, but many teams prefer explicit imports because wildcard static imports can make it less obvious where names come from.

When Static Import Helps and When It Hurts

Static import helps when:

  • the origin is already obvious
  • the imported name is conventional
  • the same member appears many times in a small scope

It hurts when:

  • the imported name is generic, such as of, create, or builder
  • multiple classes provide the same member name
  • readers have to guess where a method came from

For example, max from Math is usually fine. A mysterious parse or builder with no class name may be less clear.

Name Conflicts

Static imports can introduce ambiguity. If two imported classes expose the same static member name, Java may not know which one you mean:

java
import static java.lang.Integer.parseInt;
import static my.app.Parser.parseInt;

A call to parseInt("42") can become ambiguous. In those cases, write the class name explicitly and avoid the static import for at least one of the sources.

Common Pitfalls

The most common mistake is overusing static import until the code reads like the names came from nowhere. Readability is the goal, so when a static import makes origin less obvious, it has failed.

Another mistake is confusing import static with normal import. A normal import brings the type name into scope. A static import brings selected static members into scope.

Wildcard static imports are also easy to abuse. They save typing, but they can make large files harder to read and maintain.

Finally, do not use static import to hide poor naming. If a method name is unclear without its class, keeping the qualifier may be the better design choice.

Summary

  • 'import static lets you use static fields and methods without prefixing them with the class name.'
  • It is useful for common constants, math helpers, and test assertions.
  • The feature changes syntax, not behavior.
  • Explicit static imports are usually clearer than wildcard static imports.
  • Use static import when it improves readability, not just when it saves keystrokes.

Course illustration
Course illustration

All Rights Reserved.