Java Generic Method

In this blog post we’ll talk about java generic method: what is is, when to use it and how to write one yourself.

Before diving into this topic you might want to first read the blog post about java generic class.

Java Generic Method

A generic method in java allows you to define a method that could be called with different parameter types (e.g. String, Integer, your own defined classes). For this we can define one or more type parameters. In other words, we can define only one method that will behave the same way for different parameter types. One common example of a generic method could be a sorting algorithm. It doesn’t matter if you want to sort Strings or Integers, it will always be able to sort them as expected.

Furthermore, generic methods provide compile time safety. This means that compiler already checks during the compile time if the parameter type you are using to call the method actually matches the type that is defined in the generic method. This helps to prevent getting errors later in the runtime.

To point out, constructors could also be defined as generic.

One example of a generic method is the compareTo method of the Comparable interface. CompareTo method defines a type parameter T.

java generic method

When to Use a Generic Method?

Generic methods are used to reduce code duplication. I’m sure you have run into methods that do the same thing but just have different parameter types. This is where generic methods come into play. So, instead of defining the same method for different parameter types, you define only one by making it generic! How awesome isn’t that!?

How to Define a Generic Method?

In most cases a generic method in java is fairly simple to define. In this article we will only cover the basic example so it wouldn’t be too difficult to grasp.

Alright, let’s imagine that we need to provide functionality that could print out values of for example String, Integer and Boolean parameter types. Without using generics we could define something like the following.

void printValue(String value) {
    System.out.println("Value: " + value);
}

void printValue(Integer value) {
    System.out.println("Value: " + value);
}

void printValue(Boolean value) {
    System.out.println("Value: " + value);
}

As you can see we have defined methods for each parameter type. Now, this is where generic method comes into play. Instead of having 3 different methods, let’s define only one that will have exactly the same functionality- to print the value of the parameter it receives. To achieve this we can define a method printValue with a parameter type T.

public class GenericMethod {

    private static <T> void printValue(T value) {
        System.out.println("Value: " + value);
    }

    public static void main(String[] args) {
        printValue("Cat");
        printValue(1);
        printValue(true);
    }
}

Output:

Value: Cat
Value: 1
Value: true

In addition, we can define even more than one type parameter if needed:

public class GenericMethod {

    private static <T, V> void printValue(T value1, V value2) {
        System.out.println("Value: " + value1 + " " + value2);
    }

    public static void main(String[] args) {
        printValue("Fluffy", "Cat");
        printValue(1, 2);
        printValue(true, false);
    }
}

Output:

Value: Fluffy Cat
Value: 1 2
Value: true false

Conclusion

A generic method in java defines one or more parameter types and can be used to reduce code duplication. Common usage of generic methods are sorting algorithms. Most importantly, they provide compile time safety to prevent unexpected errors during runtime.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *