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

Name Conflicts and Ambiguity in static Imports

Handle naming collisions when multiple static imports provide methods or fields with the same name.

Name Conflicts and Ambiguity in static Imports

What you'll learn: How to handle naming collisions when multiple static imports bring in methods or fields with the same name.

The Problem: Two Sources, One Name

When you use static imports from multiple classes, you might accidentally import members with identical names. Java doesn't allow this ambiguity—if two static imports provide a method or field with the same name, the compiler won't guess which one you mean.

Think of it like this: Imagine two friends both named "Alex" walk into a room. If you shout "Hey Alex!" without clarification, both will turn around. Java refuses to operate this way—it demands you be specific.

How Conflicts Occur

A conflict happens when:

  • You static import a field or method name from multiple classes
  • Those imports bring in members with identical signatures
  • You try to use the ambiguous name without qualification

For example, if both Math and StrictMath have a sqrt method, and you static import both, calling sqrt(4.0) becomes ambiguous.

Resolving the Conflict

You have three options:

  1. Don't static import conflicting names — import only one, or neither
  2. Use fully qualified names — write Math.sqrt(4.0) explicitly where needed
  3. Remove wildcard imports — import specific members instead of import static Math.*

The compiler error will clearly identify which names conflict, so you know exactly what needs fixing.

Key Takeaway: When static imports collide on the same name, Java requires you to either avoid importing one of them or use the full class-qualified name at the call site—clarity always wins over convenience.