Results 1 to 7 of 7
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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...

    Java Code:
    public class example<T>
    {
      public example()
      {
        Class c = T.getClass();
      }
    }
    would I have to create a new instance of T...?
    new T().getClass() ...?
    Even then I foresee problems.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Generics - Class of T

    Ah... alright

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    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 {}
    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.)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,243
    Rep Power
    19

    Default 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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default 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

  1. Generics
    By pepe34 in forum Advanced Java
    Replies: 0
    Last Post: 10-30-2011, 10:31 PM
  2. generics
    By arik23 in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 04:28 PM
  3. generics
    By arik23 in forum New To Java
    Replies: 3
    Last Post: 04-19-2011, 10:31 AM
  4. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 10:12 PM
  5. Generics
    By bschmitt78 in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2010, 02:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •