Create instance of generic type in Java?

Posted by David Citron on Stack Overflow See other posts from Stack Overflow or by David Citron
Published on 2008-09-16T18:04:28Z Indexed on 2012/04/06 11:29 UTC
Read the original article Hit count: 202

Filed under:
|

Is it possible to create an instance of a generic type in Java? I'm thinking based on what I've seen that the answer is "no" (due to type erasure), but I'd be interested if anyone can see something I'm missing:

class SomeContainer<E>
{
    E createContents()
    {
        return what???
    }
}

EDIT: It turns out that Super Type Tokens could be used to resolve my issue, but it requires a lot of reflection-based code, as some of the answers below have indicated.

I'll leave this open for a little while to see if anyone comes up with anything dramatically different than Ian Robertson's Artima Article.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics