Java Generic Methods
In this page:
What is a Generic Method?
A generic method declares its own distinct type parameters inside its own signature, separate from any type parameters the enclosing class might have. This lets a single method accept and work with any data type, even inside a class that isn't generic at all.
Example: What is a Generic Method?
public class Main {
static <T> void show(T item) {
System.out.println("Value: " + item);
}
public static void main(String[] args) {
show("text");
show(42);
}
}
Login to try C/C++/Java/PHP code in the editor
Declaring Type Parameters
To define a generic method, place your type parameters inside angle brackets, such as <T>, right before the method's return type in the declaration line. This tells the compiler the parameter is scoped to that one method call, not the whole class.
Example: Declaring Type Parameters
public class Main {
static <T> T identity(T value) {
return value;
}
public static void main(String[] args) {
System.out.println(identity("scoped to this method"));
}
}
Login to try C/C++/Java/PHP code in the editor
Bounded Type Methods
You can restrict a generic method's parameters using bounded types, ensuring the method only accepts subclasses of a specific class or implementers of a specific interface. This is especially useful when the method's body needs to call a method that only exists on that bound, like compareTo() on a Comparable.
Example: Bounded Type Methods
public class Main {
static <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
public static void main(String[] args) {
System.out.println(max(5, 9));
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Method Parameters
Generic methods can declare multiple type parameters at once, separated by commas inside the angle brackets, such as <T, U>, when the method needs to relate two different input types together. A classic example is a method that copies elements from one typed list into a differently-typed destination list.
Example: Multiple Method Parameters
import java.util.List;
import java.util.ArrayList;
public class Main {
static <T, U> void copyLabeled(List<T> source, List<U> dest, U label) {
for (T item : source) dest.add(label);
}
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3);
List<String> labels = new ArrayList<>();
copyLabeled(nums, labels, "n");
System.out.println(labels);
}
}
Login to try C/C++/Java/PHP code in the editor
Type Inference
You do not need to specify type parameters explicitly when calling most generic methods, since the Java compiler automatically infers the data type from the arguments you pass in. This type inference is what keeps generic method calls looking just as clean as ordinary method calls.
Example: Type Inference
public class Main {
static <T> T pick(T a, T b) {
return a;
}
public static void main(String[] args) {
String result = pick("first", "second"); // no <String> needed, inferred
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: