• 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

Generic Classes Gotcha

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy new year Ranchers!

I've been wanting to solidify everything I know about making a generic class,
and I suddenly noticed the following is not allowed:



I thought you could do that.... since I found the code above in a mock exam,

Maybe the reason why it isn't allowed is the most obvious of the obvious; But, Could someone please give me the full detailed explanation? I'd really appreciate it.

May all your wishes come true this year,
Sincerely,
Jose
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For

new T()

tomorrow work, the type needed to be known at runtime, which is not the case (because of erasure).
 
Sheriff
Posts: 22783
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
Here's a question for you: what parameters does the constructor need? You can't assume the class you're actually using instead of T has a non-arg constructor. What if you used Integer?
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Again!

You're so full of truth Rob ! For that reason alone it shouldn't be allowed !

Is there an extra reason why that shouldn't be allowed ?

Thankful as always,

Jose
 
Rob Spoor
Sheriff
Posts: 22783
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
Well Ilja's answer is technically more correct than mine.

During runtime, there is no more T, so how will the JVM know what to create? Just create Object? That's not what you want. But what else is there? Nothing!

The same goes for other references to T that would be required during runtime:
- instanceof T
- new T[...]
- T.class
- ...

Just remember: generics only exist during compile time, so anything that would require knowledge of T during runtime is not allowed.

That's also why casting to T is allowed - casts are a compile time mechanism. It does produce a warning though since there is no way to check if the object is actually an instance of T, since instanceof is not allowed. You can get past this if you get hold of an instance of Class<T> - Class does have a means to check if an object is an instance, so it has method a method called cast(Object) that returns the object as a T.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must say now I'm completely satisfied with the answers. Of course I studied them and I believe now I can visualize the whys about not allowing that type of instantiation to happen, I'm so glad I asked.
I prefer not to extend this reply any further, I have already taken much of your precious time, so what's left for me to say is thank you very much to both of you guys and good bye!

Best Regards,
Jose
reply
    Bookmark Topic Watch Topic
  • New Topic