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 / April 2006

Tip: Looking for answers? Try searching our database.

Getting the class from a type parameter

Thread view: 
Cherukan - 18 Apr 2006 18:22 GMT
If I have a Class parameter, I can get the type of the parameter and
use it.

 void method(Class<T> clazz)
 {
   Map<String, T> t = new HashMap<String, T>();

 }

Is there any way to do the reverse, i.e. with a type variable, is there
a way
to get the Class ?

void <T> method(T t) {
   Class<T> clazz = T.class;        // does not work

   Class<T> clazz = t.getClass();  // works, but only if t is not null
}

Thx
Jeroen V. - 18 Apr 2006 18:37 GMT
> Is there any way to do the reverse, i.e. with a type variable, is there
> a way
[quoted text clipped - 7 lines]
>
> Thx

The "class of a type variable" is a Class instance representing the
*runtime* (class)type of the variable. This means that null has no Class
since it has no runtime type. I don't think there is a way to get the
static type of a variable.


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.