Generic Methods

Generic Methods

In this tutorial, we are going to discuss about Generic Methods in java. In Java, generic methods allow you to define methods with one or more type parameters. These type parameters can be used to specify the types of parameters, return types, and local variables within the method. Generic methods provide type safety and flexibility, allowing you to write reusable code that can work with different data types.

Generic Methods

1. m1(ArrayList<String>)

It is applicable for ArrayList of only String type.

2. m1(ArrayList<? extends x> l)

Here if ‘x’ is a class then this method is applicable for ArrayList of either x or it’s child classes.

If ‘x’ is an interface then this method is applicable for ArrayList of any implementation class of x

3. m1(ArrayList <? Super x> l)

If ‘x’ is a class then this method is applicable for ArrayList of either x or it’s super classes.

If ‘x’ is an interface then this method is applicable for ArrayList of any super classes of implemented class of x.

4. m1(ArrayList <?> l)

This method is applicable for ArrayList of any type.

In the method declaration if we can use ‘?’ in that method we are not allowed to insert any element except null. Because we don’t know exactly what type of object is coming.

import java.util.ArrayList;

 * @author ashok.mariyala
public class MyGenericMethod {
   public static void main(String arg[]) {
      ArrayList<String> list1 = new ArrayList<String>();
      ArrayList<Integer> list2 = new ArrayList<Integer>();

   public static void m1(ArrayList<?> list) {
      // list.add("D"); C.E: Because we can’t expect what type of value will come


[A, C, D, null]
[10, 30, 40, null]


Generics is the concept applicable only at compile time to provide type safety, at run time there is no generic concept at all.

Here’s a basic example of a generic method in Java

public class Utils {
    public <T> void printArray(T[] array) {
        for (T element : array) {
            System.out.print(element + " ");

In this example, <T> is a type parameter that represents the type of elements in the array. The method printArray can accept arrays of any type. You specify the type when calling the method, like this:

Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"apple", "banana", "orange"};

Utils utils = new Utils();
utils.printArray(intArray); // Prints: 1 2 3 4 5 
utils.printArray(strArray); // Prints: apple banana orange 

You can also use bounded type parameters to restrict the types that can be used with a generic method. For example, if you want to ensure that the elements of the array implement a specific interface or extend a particular class, you can specify a bounded type parameter like this:

public <T extends Comparable<T>> T findMax(T[] array) {
    if (array == null || array.length == 0) {
        return null;

    T max = array[0];
    for (T element : array) {
        if (element.compareTo(max) > 0) {
            max = element;
    return max;

In this example, <T extends Comparable<T>> specifies that the type T must implement the Comparable interface. This allows you to use methods like compareTo within the method body.

Generic methods are powerful tools for writing flexible and reusable code in Java. They are commonly used in utility classes and algorithms to provide type-safe operations on different types of data.

That’s all about the Generic methods in java. If you have any queries or feedback, please write us at Enjoy learning, Enjoy Java.!!

Generic Methods
Scroll to top