Why using diamond operator when instanting generic Outer class (together with Inner class) gives error in Snippet 2, while snippet 1 is perfectly fine ?
I know that rare types are prohibited, but my case is not rare types - in rare types both Outer and Inner are generic, but one of them (either one) is used as raw, and the other as generic.
Snippet 1:
class Outer {
class Inner<T> {
}
}
class Driver {
public static void main(String[] args) {
Outer.Inner obj1 = new Outer().new Inner<>(); // fine !
Outer.Inner<String> obj2 = new Outer().new Inner<>(); // fine !
}
}
Snippet 2:
class Outer<T> {
class Inner {
}
}
class Driver {
public static void main(String[] args) {
Outer.Inner obj1 = new Outer<>().new Inner(); // fine !
Outer<String>.Inner obj2 = new Outer<>().new Inner(); // error !
}
}
P.S. Tested on Eclipse compiler.
You are trying to use the diamond operator to infer the type parameter:
The JLS, section 15.9, says this about diamonds:
You have two class instantiation expressions:
and
Towards the end of section 15.9, it distinguishes between these two expressions:
Thus, the second expression is a poly expression, which means that its type depends on the assignment context; but the first expression,
new Outer<>(), is a standalone expression: its type is evaluated free from any context.Your other statement
is fine because you are using
Outeras a raw type.