0

Possible Duplicate:
How to determine the class of a generic type?

Given the code like this:

   MyClass<SomeOtherClass> = new MyClass<SomeOtherClass>();

    public class MyClass<T> implements IMyClass<T> 
    {
       public MyClass()
       {
          //I need to get class of SomeOtherClass
       }
    }

Is it possbile to do something like this in Java?

Updated for more clarity

Community
  • 1
  • 1
formatc
  • 4,261
  • 7
  • 43
  • 81

1 Answers1

1

The Java generic implementation does not allow you to know about the generic type parameters of a given instance. This is due to type erasure.

Too bad, it is not possible to get SomeOtherClass.class at your comment location.

Note that type erasure does not prevent to get the generic parameters of a declaration. For instance the following instance will be able to get the two Function interface generic parameter:

class ToStringFunction implement Function<Object,String> {
...
}   

In your case the information is available the field declaration, which is not available within your class instance.

Pierre
  • 545
  • 5
  • 14