For instance, Box<String> is translated [at compile time] to type Box, which is called the raw type -- a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime.
shwetank singh wrote:but i want an answer for something similar available for java 1.4..or does anything similar exist?..
shwetank singh wrote:as for this..what's the reason for null ownerType when the program is run?
Jim Yingst wrote:Ermmm... actually this can be done. The class file contains info about the generic types of its member variables - that info is necessary if/when you try to compile another class using classes in a jar file, for example. That info is also accessible via reflection. But only generics-related info is only available for the class in general - not for specific instances of the class. To make a concrete example:
As you see, you can get info about the declared type of instance variable fooList. You can learn that it's a List<? extends Foo>. However, if you try to examine an actual instance that is referenced by that variable, the most you can learn is that it's (in this case) an ArrayList. You can't find out whether it's an ArrayList<Foo> or an ArrayList<Bar> or an ArrayList<? extends Bar>. That specific info doesn't exist for runtime instances of a generic class. It exists for the declared type of a field, or of a method parameter or return value - but not for actual instances of the objects that are represented or referenced by those fields, parameters or return values.