What Is a Generic Method?

This section describes what is a generic method, how to declare type parameters, how to use type parameters in generic method definitions.

What Is a Generic Method? A generic method is a static method or instance method that uses one or more type parameters (also called type variables) to make it more generic so that it can be used as multiple variations, one variation for each set of specific type arguments given to type parameters.

How To Declare Type Parameters for a Generic Method? To declare type parameters for a generic method, you need to specify type parameters before the return type as a comma-delimited list enclosed in angle brackets. For example, "static <T> void someMethod(...){...}" is a static generic method with one type parameter "T" declared.

What Is a Bounded Type Parameter? A bounded type parameter is a type parameter declared with a supertype bound type with the syntax of "<T extends B>". Actually, a regular unbounded type parameter can also be viewed as a bounded type parameter with the supertype bound type equal to "Object". For example, "<T>" is equivalent to "<T extends Object>".

How Type Parameters Are Used in Method Parameter List? Once a type parameter is declared, it can be used in the parameter list definition of a generic method to help defining the type of each parameter. For example, "static <T> void someMethod(T o){...}" uses the type parameter "T" to define the type of parameter "o".

How Type Parameters Are Used in Method Return Type? Once a type parameter is declared, it can also be used in the return type definition of a generic method to help defining the type of the return value. For example, "static <T> T someMethod(){...}" uses the type parameter "T" to define the type of the return value.

How Type Parameters Are Used in Parameterized Types? Once a type parameter is declared, it can also be used as the parameter in a parameterized type to help defining types of method parameters and/or the return value. For example, "static <T> Enumeration<T> someMethod(Collection<T> c){...}" uses the type parameter "T" in two parameterized types in the method definition.

How To Specify Type Arguments When Calling Generic Methods? To specify type arguments to replace type parameters when calling a generic method, you need to provide type arguments after the dot (.) operator as a comma-delimited list enclosed in angle brackets. For example, "SomeClass.<String>someMethod(...){...}" is the expression to invoke a static generic method with one type argument "String" specified.

Why Using Generic Methods? The main advantage of using generic methods is for developing a single method to support multiple sets of data types in the method interface without using type casting. The resulting source code is type-safer and better readability.

In the next section, we will compare a generic method with a non-generic method to see what we are gaining by using generic methods.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

Generic Methods and Type Inference

What Is a Generic Method?

 Comparing Generic Method with Non-Generic Method

 Non-Generic Method Example - maxNonGeneric()

 Generic Method Example - maxGeneric()

 Generic Methods in java.util.Collections Class

 Testing Generic Methods in Collections Class

 What Is Type Argument Inference?

 Type Argument Inference by Parameter List

 Type Argument Inference by Return Value

 Generic Methods using Parameterized Types

 Parameterized Type as Generic Method Return Type

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version