Java generic methods in generics classes

后端 未结 6 1739
粉色の甜心
粉色の甜心 2020-11-29 01:49

If you create a generic class in Java (the class has generic type parameters), can you use generic methods (the method takes generic type parameters)?

Consider the f

6条回答
  •  猫巷女王i
    2020-11-29 02:01

    With Java Generics, if you use the raw form of a generic class, then all generics on the class, even unrelated generic methods such as your makeSingletonList and doSomething methods, become raw. The reason as I understand it is to provide backwards compatibility with Java code written pre-generics.

    If there is no use for your generic type parameter T, then simply remove it from MyGenericClass, leaving your methods generic with K. Else you'll have to live with the fact that the class type parameter T must be given to your class to use generics on anything else in the class.

提交回复
热议问题