2

I am learning Java generics and am confused by the method signature. It seems as if people are using it differently in every example I come across.

For example, on the site Baeldung, this is the example they give:

public <T> List<T> fromArrayToList(T[] a) {   
    return Arrays.stream(a).collect(Collectors.toList());
}

And then they go on to say this:

The <T> in the method signature implies that the method will be dealing with generic type T. This is needed even if the method is returning void.

But the <T> is not required in this example. Why?

class MyGenericClass<T>
{  
    T obj;  
    // Why does this function not need <T> before "void"?
    void add(T obj)
    {
           this.obj=obj;
    }  
    T get()
    {
           return obj;
    }  
}  
 
class Main
{  
     public static void main(String args[])
     {  
           MyGenericClass<Integer> m_int=new MyGenericClass<Integer>();  
           m_int.add(2);
           MyGenericClass<String>mstr=new MyGenericClass<String>();  
           mstr.add("SoftwaretestingHelp");
 
           System.out.println("Member of MyGenericClass<Integer>:" + m_int.get());
           System.out.println("Member of MyGenericClass<String>:" + mstr.get());
     }
} 

What does a generic method signature actually look like? I am seeing many different examples that all look different. When does a method require <T> before the return type and when does it not?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mastercooler6
  • 377
  • 2
  • 16
  • 1
    Because it is defined on the class. – Yassin Hajaj Jun 02 '22 at 12:43
  • 2
    The difference here is between a generic method, where the generic parameter is defined at the method signature, and a generic class, where the generic type is declared at the class – OH GOD SPIDERS Jun 02 '22 at 12:44
  • 1
    Related, possibly duplicate: [Java Generic Interface vs Generic Methods, and when to use one](https://stackoverflow.com/questions/30575434/java-generic-interface-vs-generic-methods-and-when-to-use-one), [choosing between class level or method level generics](https://stackoverflow.com/questions/39679876/choosing-between-class-level-or-method-level-generics) – Mark Rotteveel Jun 02 '22 at 12:48

2 Answers2

3

In that example, the class is made generic, so T is specified when you instantiate it. The method is different because it need not be in a generic class.

ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • 1
    So if the class was not generic, then I would need to put the before the return type I assume? – mastercooler6 Jun 02 '22 at 12:45
  • 1
    Even a method in a generic class could have a generic type definition, e.g. if the method itself needs additional or other generic types than those defined in the class. – Mark Rotteveel Jun 02 '22 at 12:46
1

Having method such this:

public <T> List<T> fromArrayToList(T[] a) {   
    return Arrays.stream(a).collect(Collectors.toList());
}

Makes only this method generic and T is valid in method domain.

But this one is class level generic that makes T available in any method of class.

class MyGenericClass<T>
{  
    T obj;  
    // Why does this function not need <T> before "void"?
    void add(T obj)
    {
           this.obj=obj;
    }  
    T get()
    {
           return obj;
    }  

    List<T> fromArrayToList(T[] a) {   
           return Arrays.stream(a).collect(Collectors.toList());
    }
}

I have added your method in class and you can better figure out difference. As you see in method level generic is declared in method signature but in class level generic is declared in class signature.

So in first case you have Generic Method and in second case you have Generic Class

Obviously you use declare generic method when you need make only a method generic, but generic class is used in cases that generic type need to be used in different methods of class.

Eskandar Abedini
  • 2,090
  • 2
  • 13