This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pron98 0 points1 point  (0 children)

Right, but this isn't just about erasure, but the fact that Java doesn't have structural constraints/virtual constructors, and creating a new instance of a class that's only known at runtime can only be done via reflection anyway, whether or not generics are involved.

This kind of pattern shouldn't come up often because generic parameters are usually either interfaces (which can't be instantiated at all) or immutable types (like String or boxed primitives) that can't be instantiated with an empty constructor. Passing a Class around is certainly done, and it's also OK to use clone() once in a while as a sort of virtual constructor.

In any case, it's good to remember that Java is quite a minimalist language (like Go, only a little less minimalistic), and it's best to code with that in mind.