Find out the real type in a generic class.

If I have an class that will take an generic type. How can I figure out what is the real type for my instance.

I want to replace the TODO in my example with some code so the example will generate this output:

class java.lang.Integer

class java.lang.Double

My example code:

public class Foo<N extends Number> {

void bar() {

Class c = TODO;

System.out.println("My type is: " + c);

}

public static void main(String[] args) {

new Foo<Integer>().bar();

new Foo<Double>().bar();

}

}

[585 byte] By [smassemana] at [2007-11-26 18:40:36]
# 1
This is not possible. All instances of Foo are created from the same class. An instance of Foo<Integer> have the same class as an instance of Foo<Double>.
smassemana at 2007-7-9 6:14:37 > top of Java-index,Core,Core APIs...
# 2
Yes, buddy.You got things right...Generics is just a compile time notion.The TypeErasure Concept over here gives you a better idea of what really happens http://java.sun.com/docs/books/tutorial/java/generics/erasure.html
maniam_a at 2007-7-9 6:14:37 > top of Java-index,Core,Core APIs...