generic methods can be defined in a normal class or in a generic class
1 class arrayalg{2 Public static <T> T getmiddle (t...a) {3 return a[a.length/2]; 4 }5 }
Note: Type variables are placed after modifiers (public static)when a generic method is called, the method name is placed in a specific type before the angle brackets
String middle=arrayalg.<string>getmiddle ("John", "Q.", "public");
in most cases, the method call can omit the <String> type parameter
String middle=arrayalg.getmiddle ("John", "Q.", "public");
qualification of type variablessometimes a class or method needs to constrain a type variable
1 classarrayalg{2 Public Static<T>T min (t[] a) {3 if(a==NULL|| a.length==0)return0;4T smallest=a[0];5 for(inti=1;i<a.length;i++)6 if(Smallest.compareto (A[i]) >0)7smallest=A[i];8 returnsmallest;9 }Ten}
in the example above, the variable smallest type is T, and the smallest must have a CompareTo method to run properlythe workaround is to limit t implementation of the comparable interface
Public Static extends comparable> T min (t[] a) ...
a type variable or wildcard can have more than one qualification, with a qualified type of & delimited
extends Comparable & Serializable
Java generic methods and type qualification