Results 1 to 7 of 7
Thread: Generics - Class of T
- 12-31-2012, 01:26 AM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Generics - Class of T
If I have a generically typed class with a type T, how do I determine the type of (that is, get a Class instance of) T at runtime?
Something like...
would I have to create a new instance of T...?Java Code:public class example<T> { public example() { Class c = T.getClass(); } }
new T().getClass() ...?
Even then I foresee problems.
-
Re: Generics - Class of T
You can't. Generics are compile-time constructs only, not run-time. Read up on type erasure for more on this.
- 12-31-2012, 07:51 AM #3
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: Generics - Class of T
Ah... alright
- 12-31-2012, 08:15 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Generics - Class of T
Notice that at compile time you know the type T. That is, you would declare a variable or return type as Example<String>, or Example<MyClass> or whatever. It is quite allowable to pass a Class<Whatever> to the constructor.
I'm not sure if that helps. Perhaps if you said what you are trying to do (as distinct from how you are trying to do it.)Java Code:public class Eg<T> { T data; Eg(Class<? extends T> claz) throws Exception { T data = claz.newInstance(); } public static void main(String[] args) throws Exception { Eg<Super> test = new Eg<Super>(Sub.class); // or Super.class } } class Super {} class Sub extends Super {}
- 12-31-2012, 08:36 AM #5
Re: Generics - Class of T
I had the same question once. There's a sneaky way to get at the class by using a (possibly anonymous) subclass, discussed here: https://forums.oracle.com/forums/thr...readID=2244865
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-01-2013, 01:10 AM #6
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: Generics - Class of T
Hmm, I'll read that thread a bit. I was just experimenting with reflection, wasn't really trying anything specific per se. Just nice to know little tricks should the need to use them come up in the future.
- 01-13-2013, 04:45 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 703
- Rep Power
- 1
Re: Generics - Class of T
Not certain how to cleanly do what you want (or uncleanly for that matter) because of type erasure. And even if you could get a type for T (e.g. String, Integer), Collections are not covariant so the type of any List<T> is simply List. But I did find an interesting article which directly addresses your issue. It can be viewed at Neal Gafter's blog: Reified Generics for Java.
v/r,
Jim
Similar Threads
-
Generics
By pepe34 in forum Advanced JavaReplies: 0Last Post: 10-30-2011, 10:31 PM -
generics
By arik23 in forum New To JavaReplies: 2Last Post: 04-21-2011, 04:28 PM -
generics
By arik23 in forum New To JavaReplies: 3Last Post: 04-19-2011, 10:31 AM -
Generics
By sakshamkum in forum Advanced JavaReplies: 3Last Post: 06-03-2010, 10:12 PM -
Generics
By bschmitt78 in forum Advanced JavaReplies: 3Last Post: 03-16-2010, 02:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks