Amit
Amit

Reputation: 604

Java generic at return type

I have seen some code like this and unable to understand its significance:

public class ClassA{

public <T> void getContactName(ContactList<T> contactList){
    //do something
}    

}

Basically I didn't understand this. The class compiles without any error. I thought ClassA should also be made generic with parameter 'T' .

Thanks

Upvotes: 2

Views: 122

Answers (3)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

The definition

public <T> void getContactName(ContactList<T> contactList){
    //do something
}    

means that only the method is generic and the type with a name T is valid only in the scope of the method. There's no need the class to be generic if the T type parameter is used only in a single method.

As a side note, remember that in Java you can make generic:

  • classes (except the anonymous ones)
  • methods
  • interfaces

but you can't make generic:

  • exceptions
  • anonymous classes
  • enums

Upvotes: 6

Igor Mielientiev
Igor Mielientiev

Reputation: 586

According to Java Language Specification:

  • A type variable is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
  • A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.
  • An interface is generic if it declares one or more type variables. These type variables are known as the type parameters of the interface. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.
  • A method is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.
  • A constructor can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.

You can use your method like this:

new ClassA().<String>getContactName(contactList);

or you can not specify the type parameter

new ClassA().getContactName(contactList);

You can read specification for further details and good faq you can find here

Upvotes: 1

Braj
Braj

Reputation: 46841

It's better explained under Java Tutorial on Generic Methods and Generic Types along with detail examples and uses of generic methods.

here is an example (build-in Arrays class). Have a look at the method signature that ensures that method return type is exactly same as method arguments since class itself is not generic but you can make method as generic.

class Arrays {
    public static <T> List<T> asList(T... a) {
    ...
}

You can create static generic utility methods as mentioned above where you don't need to create object of the class.

Upvotes: 2

Related Questions