Java-Generic Method

Generic Method

It is possible to declare a generic method that uses one or more type parameters of its own. Furthermore, it is possible to create a generic method that is enclosed within a non-generic class. For example, in the following program declares a non-generic class called ShowDataType and a generic method within that class called showType( ). The showType( ) method shows which type object passed to it. It can be used with any type of object. Notice how showType( ) is declared :

The type parameters are declared before the return type of the method.



Program Source

class ShowDataType {
    <T> void showType(T ob)
        System.out.println("T Type : "+ob.getClass().getName());

public class Javaapp {
    public static void main(String[] args) {
        ShowDataType sd = new ShowDataType();

Leave a Comment