简体   繁体   中英

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

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;
    }
}

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.

ie 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 (ie the name of some class or interface), and not a generic type parameter.

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. ie <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.

There is two aspects to this:

A)

Instance methods implicitly acquire all the class type-parameters whereas the static don't. The reason for this is that at compilation time the type assigned to those type-parameters are specific to each object instance reference, so they would never apply to static methods since these don't have a particular instance associated to them.

B)

All methods, instance or class ones can defined additional type-parameters that only apply to that method. These are placed between the modifiers ( public , static , ...) and the return type. That is just an arbitrary syntax choice made by the Java language developers. So they could have done it differently, however it makes more sense to have to declare something before it is ever used (eg in the parameter type declaration).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM