How to interpret “public T readObjectData(… Class type)” in Java?

前端 未结 4 1086
不知归路
不知归路 2020-12-02 11:20

I have this Java code.

public  T readObjectData(ByteBuffer buffer, Class type) {
...
T retVal = (T) summaries;
return retVal;
4条回答
  •  暖寄归人
    2020-12-02 12:06

    You are probably confused by a similar and more common declaration:

    class MyClass {
       private  T myMethod(T a){
           return  a;
       }
    }
    

    In above case, there is no need for "" after private ( private T myMethod(T a) )

    because the T it's using is the same than the one defined in the class MyClass

    Even more, if you write

    class MyClass {
       private  T myMethod(T a){
           return  a;
       }
    }
    

    then the meaning is that the myMethod return type is (potentially) different than the MyClass type. As if you wrote this instead:

    class MyClass {
       private  T2 myMethod(T2 a){
           return  a;
       }
    }
    

    Credits: Took the example from "Kanagavelu Sugumar"'s longer answer to How to use Class in Java?

提交回复
热议问题