2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Get generic type of class at runtime

public class GenericClass<T>
{
    public Type getMyType()
    {
        //How do I return the type of T?
    }
}

From this I want to get the specific callable class name.But when every time it resulting me the object type.

userimage

it's only possible via reflection in certain circumstances.

If you really need the type, this is the usual (type-safe) workaround pattern:

public class GenericClass<T> {

     private final Class<T> type;

     public GenericClass(Class<T> type) {

          this.type = type;

     }

     public Class<T> getMyType() {

         return this.type;

     }

}

Answer is