Originally posted by Garrett Rowe:
Here <T> is unknown until the left-hand side of the assignment operator is evaluated, after the method call returns.
Yeah, that's what I thought you meant. It
is strange looking! Even stranger (until you think about it) is the fact that a bare call like
FinalTest.newList()
compiles too!
But the thing is that generics work by "erasure" which means that in reality, an ArrayList<
String> is identical to an ArrayList<Object>. There are annotations in the class files that help the compiler reconcile types, but ultimately, at runtime, the types are gone. So if you write a method that returns a parameterized value with a free type parameter like this, then the compiler can simply treat it as a value of the "correct" type.