wood burning stoves 2.0*
The moose likes Java in General and the fly likes Generics version of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics version of "new T()"?" Watch "Generics version of "new T()"?" New topic
Author

Generics version of "new T()"?

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Just a hypothetical question, but assuming that a generic type T has a concrete implementation and a no-arg constructor, is there a clean way to get a new instance?
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

David O'Meara wrote:Just a hypothetical question, but assuming that a generic type T has a concrete implementation and a no-arg constructor, is there a clean way to get a new instance?

http://forums.sun.com/thread.jspa?threadID=769984
The URL above seems to has some discussion on that though it was quite old.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Raymond is right. The only way to instantiate any T instance is through a Class<T>. Either use newInstance() for getConstructor(...).newInstance(...).
Similarly, the only way to instantiate any T[] is to use java.lang.reflect.Array with a Class<T>. You'll still need to do a cast, suppressing the warning, but that's safe:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The reason it is more theoretical is that it is currently a utility method used to map from a DB entity to a POJO (yes I know they can be detached, long story) for a variety of enumerated types. More or less.


I currently use it like this, which is not terribly difficult

where there are several similar types for MyValue and MyItem
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Is that something you'd prefer?

I guess you want something more like where it uses type inference to determine T and instantiate it, but that's simply not possible.


Perhaps generics can be extended in the future, something like this:
Omitting the constructor argument types will then use the current situation, where instantiation is impossible.

However, because of type erasure I doubt this will ever happen. T is no longer known at run time. For this to work the methods would need to be inlined, as that's the only way to link T and its actual value. Or type erasure needs to be removed, but that would cause a lot of compatibility issues.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you have something like this

then you can extract the type T from the supertype provided the supertype contains the generic binding using the following:

and would then be able to use myType.newInstance(), but not really applicable to my situation.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

But I haven't found a way to do the same when the class is declared without a binding on the supertype
 
wood burning stoves
 
subject: Generics version of "new T()"?
 
Similar Threads
WA #1.....word association
Thread class
Determining occurrences of a character in a string
shortcut in Mozilla Browser..
Thread & Synchronization