-2

As the title states, why can't Java generics be used for static methods? enter image description here

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Jswq
  • 758
  • 1
  • 7
  • 23

1 Answers1

5

<T> is the generic type of the class. You cannot refer to it from a static context, since each instance of the class may have a different T (similar to why you can't reference instance members from static methods). You could, however, give the method itself a generic type:

public static <S> S test (S s) {
    // code...
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350