• Post Reply Bookmark Topic Watch Topic
  • New Topic

instantiate generic type  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something like this:



how can I instantiate T within MyClass?

Like this?:


this does not work:

 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ConstructorSet methodGet methodNo, I don't think you can say new T(). That is because the type of T is determined from outside the class.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nimo,

take a look at this article on Wikipedia! It explains why the type information of generics are not available in the same way as template parameters in C++ for example ;-) In short it's because Java generics are implemented with type erasure to maintain backward compatibility for collections etc.

Additionally you can't write something like T.someMethod() because of this.

Marco
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with (T)new Object() is that T can be something completely different - like String. You then try to treat the Object as a String, and you will get a ClassCastException.

Now you would like something like new T(), but that has a problem as well - it assumes there is a parameter-less constructor. If T is Integer, that assumption is false.


There is one way to create T instances, but that requires a Class<T>. With that, you can call newInstance(), although that has the same problem as new T(). Integer.class.newInstance() will fail, for instance. The fully safe* way is use the Class<T>'s getConstructor(...) method. You can get the parameter types using the getConstructors() method.
Now ideally getConstructors() would return Constructor<T>[] instead of Constructor<?>[], but for some reason unknown to me Sun didn't, even though each element must be a Constructor<T> - after all, how can a constructor not create an instance of the class itself?


* Of course, this does not prevent the constructor throwing exception if incorrect values are passed.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pedantic mode>
"do" is a reserved keyword
</pedantic mode>
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:<pedantic mode>
"do" is a reserved keyword
</pedantic mode>
So it is. Thank you. I forgot.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the special colouring for it already was a big enough clue so I didn't bother mentioning it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!