Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Generics and type erasure

Thread view: 
Martin Lorentzson - 05 Nov 2006 21:56 GMT
I would like to determine the class value of a generic type.  Is it
possible?

Right now, I'm passing the type as a parameter to the class
constructor:

public class Example<T extends Enum<T>> {
       
   private final Class<T> clazz;

   protected Example(Class<T> clazz) {
    this.clazz = clazz;
   }
       
   public T parse(String text) {
       // Can I derive the value of clazz from T?
    T value = T.valueOf(clazz, text);
    return value;
   }
}

But I'm wondering if I can derive the class value of T instead of
using a constructor parameter or is type erasure as play here?

/Martin
Daniel Pitts - 05 Nov 2006 22:17 GMT
> I would like to determine the class value of a generic type.  Is it
> possible?
[quoted text clipped - 21 lines]
>
> /Martin

In fact, you cannot.  That is the whole point(problem) of erasure.
Even the EnumMap and EnumSet classes require a Class object in their
construction.
Martin Lorentzson - 06 Nov 2006 21:02 GMT
> In fact, you cannot.  That is the whole point(problem) of erasure.

OK, I thought I figured as much from the docs but I just wanted to
make sure.  Thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.