Stephen Bannasch wrote:
>> Hi all,
>>
>> I'm having a hard time loading the '.class' object of a java generic class. 
>> In the older java if I had a class Student, I could simply call 
>> Student.class and get its Class object. But now if I have a Class <A <B>>, 
>> I've tried all the possible ways I could think of to get it's class object 
>> but I wasn't able. I've tried the net as well but I was out of luck :(.... 
>> My work is kind of halted unless I solve this little problem so if anyone 
>> has more experience with Java Generics, your help is much appreciated.
>>     
>
> Can you show the simplest code which demonstrates the problem?
>   

My guess is that you want to get the class object from a type parameter. 
That is something I wanted to do too, but that is actually not possible. 
The fundamental reason is that the type information for generics is not 
available at runtime.

For example the following isn't possible:

    <T> void addNew(List<T> list) throws Exception{
           list.add(T.class.newInstance());
       
    }


If you need to get the class from a type parameter, the best you can do 
is pass it in.  So the
method above would look like this:

    <T> void addNew(List<T> list, Class<T> klass) throws Exception{
         list.add(klass.newInstance());
    }

Scott

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SAIL-Dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/SAIL-Dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to