wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Generic Classes Gotcha Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic Classes Gotcha" Watch "Generic Classes Gotcha" New topic
Author

Generic Classes Gotcha

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
For

new T()

tomorrow work, the type needed to be known at runtime, which is not the case (because of erasure).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Oct 27, 2005
Posts: 19682
    
  20

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

Joined: May 28, 2007
Posts: 339
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic Classes Gotcha