Consider the following code snippet.
package spoon; import rx.Observable; import java.util.*; public class Test { public void test() { Observable o = new Observable(); } }
This code compiles well with Java 11 even though I thought it should not. Observable
is also a java class in java.util
and I do not understand how the compiler knows whether to use Observable
from java.util
or my custom package rx
.
Note that java.util.Observable
was deprecated in Java 9, however, I don’t think so it should have any effect on imports.
Advertisement
Answer
The single name import wins over the wildcard (called “import-on-demand” by the standard).
To cite chapter and verse:
https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.5.2
“The declaration might be shadowed by a single-type-import declaration of a type whose simple name is…”
https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.4.1
“A type-import-on-demand declaration never causes any other declaration to be shadowed. “