Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-syntax for explicitly specifying generic arguments in method calls

Tags:

java

generics

What is the syntax for explicitly giving the type parameters for a generic Java method?

like image 783
Hans-Peter Störr Avatar asked Jun 10 '10 08:06

Hans-Peter Störr


People also ask

Which of the following is correct generic method syntax in Java?

4. Which of these is an correct way of defining generic method? Explanation: The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.

What is the syntax for declaring a generic class?

Syntax to Create Objects of Generic Class Where generic_class_name is the name of the generic class, type is the data type such as Integer, String, user-defined data types, etc., passed as a parameter to the generic class and new is the keyword used to create an object or instance of the generic class.


2 Answers

According to the Java specification that would be for example:

Collections.<String>unmodifiableSet() 

(Sorry for asking and answering my own question - I was just looking this up for the third time. :-)

like image 66
Hans-Peter Störr Avatar answered Oct 04 '22 20:10

Hans-Peter Störr


The following is not the syntax

<ArgType>genericMethod() 

It seems the type arguments must come after a dot as in

SomeClass.<ArgType>genericMethod() this.<ArgType>genericMethod() p.<ArgType>genericMethod() super.<ArgType>genericMethod() SomeClass.super.<ArgType>genericMethod() SomeClass.this.<ArgType>genericMethod() 
like image 36
Theodore Norvell Avatar answered Oct 04 '22 21:10

Theodore Norvell