Java language
Generic method declaration
Declares a method-specific type parameter; type arguments can be inferred or supplied explicitly.
Formal Syntax & Structure
java syntaxjava
static <T> T read(Repository<T> repository)Working Example
Example Snippetjava
public class Main { interface Repository<T> { T find(); } record Single<T>(T value) implements Repository<T> { public T find() { return value; } } static <T> T read(Repository<T> repository) { return repository.find(); } public static void main(String[] args) { Repository<String> names = new Single<>("Atlas"); Repository<Integer> quantities = new Single<>(7); System.out.println(read(names)); System.out.println(Main.<Integer>read(quantities)); }}