• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics version of "new T()"?

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic