Course contentsShow
← Java
Lesson 378 of 2,8709. Static Members and Utility ClassesFree lesson

Wildcard static Imports

Use wildcard syntax to import all static members from a class and understand when this is appropriate.

Wildcard static Imports

You'll learn how to use the wildcard * to import all static members from a class at once, and when this shortcut makes sense.

The Core Idea

You've already learned to import individual static members like this:

import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;

But what if you need many static members from the same class? Java lets you import them all at once using the wildcard syntax:

import static java.lang.Math.*;

This single line imports every static field and method from Math—PI, E, sqrt, pow, sin, cos, and so on. You can now use them all without the Math. prefix.

When to Use Wildcards

Use wildcard static imports when:

  • You're using many static members from the same utility class
  • The imported names won't clash with your own variables or methods
  • It improves readability (e.g., mathematical formulas become cleaner)

Avoid them when:

  • You only need one or two members (be explicit!)
  • Imported names might conflict with local variables or methods from other classes
  • It makes code harder to trace (readers can't tell where sqrt comes from)

Example Comparison

Without wildcard:

double result = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

With wildcard static import:

import static java.lang.Math.*;

double result = sqrt(pow(x, 2) + pow(y, 2));

The second version reads more like a mathematical formula, which can be clearer in math-heavy code.

Key Takeaway: Wildcard static imports (import static ClassName.*;) import all static members at once—convenient for utility classes you use heavily, but use sparingly to avoid namespace pollution and confusion.