Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why static generic method should have<T> in front of return type

Tags:

java

generics

Why a generic static method have a in addition to return type, but a instance method not?

public class Main<T> {
    public  static <T> T met(T t) {
        return t;
    }

    public  T met1(T t) {
        return t;
    }
}
like image 933
JHunter Avatar asked Dec 06 '22 13:12

JHunter


2 Answers

An instance method can also define the generic type parameter (<T>) in front of the return type, but it doesn't have to, since it can use a generic type parameter already defined in the class level (public class Main<T>).

On the other hand, a static method cannot use the generic type parameter defined in the class level, so it must declare any generic type parameter it intends to use.

i.e. both of the following are valid

public static <T> T met(T t) {
    return t;
}

public <T> T met1(T t) {
    return t;
}

On the other hand, in the following

public static T met(T t) {
    return t;
}

T is assumed to be a type identifier (i.e. the name of some class or interface), and not a generic type parameter.

like image 185
Eran Avatar answered Dec 27 '22 10:12

Eran


First, we need to understand what is the "addition". Is it NOT an addition to the return type. It is a "Bounded Type Parameters"

Bounded Type Parameters There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.

When you compile your generic class/method/interface. Java compiler converts generic type to code that JVM understands. This process is call type erasure and it requires to know bounds of generic's type parameters. i.e. <T> get converts to Object because it is unbounded and <T extends Comparable<T>> get converts to Comparable


Second, why does a generic static method requires bounded type parameters whereas a generic instance method does not?

This kind of goes hand in hand with the difference between class method and instance method.

  • When you use the key word static the method becomes a class method. Which means you can invoke without a creating an instance. And that is the problem. Because static method is shared among all instances of the class including instances of different type parameters, Java doesn't know what T is until you instantiate a type. We need to explicitly tell the compiler what instance the class method should expect.

  • When you don't use the key word static the method is now an instance method. Which means you can't invoke the method until you create an instance of the class. When creating an instance you would need to specify the type parameter. Java compiler can inherently use that type parameter when you invoke instances method so bounded type parameter is optional for instance method.

like image 23
OLIVER.KOO Avatar answered Dec 27 '22 11:12

OLIVER.KOO