What search order does the Java compiler use to resolve Foo in the following class?
class Test
{
Foo f;
}
Empirical tests reveal the following search order:
- Nested classes
- Superclass nested classes
- java.lang.* classes
- Others?
but I’d like to know where this is discussed in the Java Language Specification.
I don’t think that there is a search order in the sense that you mean. Rather, I think that this rule applies:
In this context, imports count as declarations; see JLS 6.1. However, the complicating factor is the Shadowing rules (JLS 6.3.1) which say that some kinds of declarations (of types in this case) hide existing declarations, and others don’t. Specifically, an “import on demand” (e.g.
import java.util.*;or the implicit import ofjava.lang.*) do not shadow other declarations.So for example;
The other wrinkle is that a “single-type-import” (like
import java.sql.Date;above) shadows types declared in the same package, and types imported on demand, but it does not shadow other types imported via another “single-type-import” (JLS 6.3.1). So for example, the following is a compilation error: