Consider the following pathological example:
class Ideone {
static class ArrayList<T> {
ArrayList() {
System.out.println("!!");
}
}
static class java {
static class util {
static class ArrayList<T> {
ArrayList() {
System.out.println("Here");
}
}
}
}
public static void main(String[] args) {
new ArrayList<>();
new java.util.ArrayList<>();
// Can I refer to the "usual" java.util.ArrayList?
}
}
The two instances created in the constructor are of the nested classes.
But how might I refer to the java.util.ArrayList
that we all know and love in the same class? We can't import it, and we can't use the fully-qualified name, as the nested class symbols would be used instead.
What can we do in this case? (Other than the obvious - stop using such wantonly evil names for the nested classes).
You can no longer directly reference
java.util.ArrayList
if you've done the 2 things you've done:ArrayList
with a static nested class in scope.java.util.ArrayList
with a classArrayList
nested within classutil
, nested within nested classjava
.You can't even "split" the import in an attempt to use a "partially qualified" import.
You can
import java.*;
, but that is worthless; no classes are defined in thejava
package directly.However, you can reference the class
java.util.ArrayList
indirectly because it's notfinal
. Outside the scope of the classIdeone
, declare a subclass with a different name.Then you can refer to that class and program to the interface: