• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics version of "new T()"?

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I haven't found a way to do the same when the class is declared without a binding on the supertype
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic