Sub topic 9
Resolving Name Conflicts in Java
This chapter assumes you can declare packages, compile basic Java classes, and use single-type and wildcard imports. A name conflict occurs when two accessible types have the same simple name, such as java.util.Date and java.sql.Date.
Simple names and fully qualified names
A simple name is the class name without its package:
Date today;A fully qualified name includes the package:
java.time.LocalDate today;Java uses the fully qualified name to identify the exact type. Imports only provide shorter names for types.
When two classes have the same simple name, import one of them and write the other with its fully qualified name:
import java.util.Date;
public class DateConflict {
public static void main(String[] args) {
Date utilDate = new Date();
java.sql.Date sqlDate =
new java.sql.Date(utilDate.getTime());
System.out.println(utilDate.getTime() == sqlDate.getTime());
}
}Here, Date means java.util.Date because of the import. The expression java.sql.Date identifies the other class explicitly.
What happens when imports conflict?
Java does not allow two single-type imports with the same simple name:
import java.util.Date;
import java.sql.Date; // Compile-time errorThe compiler cannot choose which Date you mean. Remove one import and use its fully qualified name where needed.
Wildcard imports behave differently:
import java.util.*;
import java.sql.*;
public class AmbiguousDate {
Date value; // Compile-time error
}Neither wildcard import wins. The simple name is ambiguous because both packages provide a type named Date.
A single-type import takes priority over a wildcard import:
import java.util.Date;
import java.sql.*;
public class ClearDate {
Date value; // java.util.Date
}The explicit import identifies Date, while the wildcard makes other java.sql types available.
| Situation | Result |
|---|---|
| One single-type import | The simple name refers to that type |
| Two single-type imports with the same name | Compile-time error |
| Two wildcard imports with the same type name | Ambiguous simple name |
| Single-type import plus wildcard import | The single-type import wins |
Shadowing inside your code
Shadowing occurs when a nearer declaration hides a declaration with the same name. A local variable can shadow a field:
public class ShadowingDemo {
private String name = "field";
public void printName() {
String name = "local";
System.out.println(name);
System.out.println(this.name);
}
public static void main(String[] args) {
new ShadowingDemo().printName();
}
}The unqualified name refers to the local variable. this.name explicitly refers to the field belonging to the current object.
Package names and type names can also become confusing when a project uses short, generic names. Prefer clear names and avoid creating classes that duplicate commonly used platform types.
Choosing a practical solution
Use a single-type import when a class is used repeatedly and its name is unambiguous. Use a fully qualified name when two similarly named classes appear in the same file or when the distinction improves readability. Static imports can create similar conflicts between methods or constants, so use them only when the source remains clear.
Exercise
How can a file use both java.util.Date and java.sql.Date?
Show answer
Import one type, then refer to the other with its fully qualified name, such as java.sql.Date.