Scope of Generic Method Type Parameters

Java-Generic Methods

Generic Methods

It is possible to declare a generic method that uses one or more type parameters of its own. You can define generic methods in an ordinary or generic class. Methods within a generic type definition can also have independent type parameters. Generic methods are smarter and can figure out their parameter types from their usage context without having to be explicitly parameterized. (In reality, of course, it is the compiler that does this.) This is called type argument inference. In addition, if a generic method is static, it has no access to the generic type parameters of the class, so if it needs to use genericity it must be a generic method.

Like generic classes, generic methods have a parameter type declaration using the <> syntax. To define a generic method, you simply place a generic parameter list before the return value, like this :

generic method

This showUV( ) method has its own parameter type declaration that defines the type variable U and V. This method is a generic method and can appear in either a generic or nongeneric class. The scope of U and V is limited to the method showUV( ) and hides any definition of U and V in any enclosing generic class. As with generic classes, the type U or V or both can have bounds :

Generic Methods

Unlike a generic class, it does not have to be instantiated with a specific parameter type for U and V before it is used. Instead, it infers the parameter type U and V from the type of its arguments, entry. This is called type argument inference. For example :

Generic Methods

The showUV( ) calls look like normal method calls. For the calls to showUV( ) that use primitive types, autoboxing comes into play, automatically wrapping the primitive types in their associated objects. In fact, generic methods and autoboxing can eliminate some code that previously required hand conversion.

The following program declares a nongeneric class called GenMethDemo and a static generic method within that class called showUV( ). The showUV( ) method prints the U and V types. Notice that the type V is upper-bounded by Number. Thus, V must be the type of Number. The Number is a superclass of all numeric classes, such as Integer, Float and Double . In the first call to showUV( ), the type of the first argument is String , which causes String to be substituted for U. The second argument is Integer , which makes Integer a substitute for V, too. In the second call, Integer and Double are used, and the types of U and V are replaced by Integer and Double.

Although type argument inference will be sufficient for most generic method calls, you can explicitly specify the type argument if needed. Notice the third call to showUV( ) in main( ). The type arguments are specified. Of course, in this case, there is nothing gained by specifying the type arguments. Furthermore, JDK 8 improved type inference as it relates to methods. As a result, there are fewer cases in which explicit type arguments are needed.

Program

Generic Methods

Program Source

class GenMethDemo {
    
    static <U,V extends Number> void showUV(U u,V v) {
        
        System.out.println("AnyType    : "+u.getClass().getName());
        System.out.println("NumberType : "+v.getClass().getName());
    }
}

public class Javaapp {
    
    public static void main(String[] args) {
        
        GenMethDemo.showUV("JAVA",11);
        GenMethDemo.showUV(50,90.5);
        GenMethDemo.<Float,Double>showUV(50.5f,7.5);
    }
}

Leave a Comment