0

I have such code:

public class GenClass<T> 
{
    public static void m() 
    {
        //I need to know here, what is T ?
    }
}
class Test
{
    class Cl1 extends GenClass<Integer>{}
    class Cl2 extends GenClass<MyClass>{}
    class Cl3 extends GenClass<Cl3>{}

    public static void main(String[] args) 
    {
        Cl1.m();
        Cl2.m();
        Cl3.m();
    }
}

How can I know inside method m, what is T?

PS: I'm very new to java (my language is C#). I've found such construction as a solution of similar problem:

return (Class) ((ParameterizedType) /*getClass()*/GenClass.class
    .getGenericSuperclass()).getActualTypeArguments()[0];

But it was about instance method, so as I can't call getClass() from static method, I've tried to replace it by GenClass.class, but looks like .class eveluates statically, so it probably have not any info about T.

user1234567
  • 3,991
  • 3
  • 19
  • 25
  • 1
    [it's not easy](https://stackoverflow.com/questions/64873444/generic-class-type-parameter-detail-at-runtime/64934396#64934396) – Eugene Jun 25 '21 at 17:18
  • @Eugene, thank you for your answer, but (as it looks for me) my case is different, because I need to know `T` inside static method, so I have not place to place field, which will remember T – user1234567 Jun 25 '21 at 17:28
  • 2
    T isn’t defined in a static context. It’s generic, and each instance of the class could potentially have a different type of T. – BeUndead Jun 25 '21 at 17:35
  • @BeUndead, looks like your comment - is the best answer. If you make it an answer, i'll accept it – user1234567 Jun 30 '21 at 10:21

0 Answers0