Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Muskar View Post
    My problem is that I don't have an object of T in my class and I'm not planning on getting one. I have an ArrayList<T> though - but I'm not aware of any way to get it's parametrized type - and the list is allowed to be empty, so I can't just get an object inside it.
    Ah, OK. Norm is right, generics is a compile-time thing, erasure removes it from the runtime (mostly), so you need an object to work with.

    There is a way to retain generic types for classes, which relys on the need for anonymous classes to retain their generic type information at runtime.

    I only browsed through the earlier part of the thread, so this may already have been mentioned - ignore the following if so...

    Using this technique, you could subclass a collection class such as ArrayList, to make the generic type available at runtime. See Using Type Tokens To Retrieve Generic Parameters and Super Type Tokens.

    It's a bit of a hack though...
    Last edited by dlorde; 06-04-2011 at 11:52 AM.

  2. #22
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    Ah, OK. Norm is right, generics is a compile-time thing, erasure removes it from the runtime (mostly), so you need an object to work with.

    There is a way to retain generic types for classes, which relys on the need for anonymous classes to retain their generic type information at runtime.

    I only browsed through the earlier part of the thread, so this may already have been mentioned - ignore the following if so...

    Using this technique, you could subclass a collection class such as ArrayList, to make the generic type available at runtime. See Using Type Tokens To Retrieve Generic Parameters and Super Type Tokens.

    It's a bit of a hack though...
    Thanks it made me understand what the problem with type erasure is. It makes me a bit sad though, since my work will not be as awesome, and easy to implement for users, as intended but I think I'll create constructors to open up for both solutions:
    • Taking the class as a parameter in the constructor
    • Or requiring instantiation of my class to be anonymous

    So people can do what they find easiest - although I would've liked to be able to have one with an empty constructor since it would be easier for newbs.

    Anyway, I'll edit this post with the code if my solution will work. Otherwise I'll probably do another post about my problems.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Class as a Parameter.
    By Soxman04 in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 06:36 PM
  2. Class<T> in method parameter
    By Onra in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 12:12 AM
  3. doubt regarding <type parameter>
    By subith86 in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 03:35 PM
  4. Passing final parameter from a class
    By ianyappy in forum New To Java
    Replies: 8
    Last Post: 12-02-2010, 07:06 PM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 01:44 PM

Tags for this Thread

Posting Permissions

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