Reputation: 8802
Using Java 1.7 compiler, it is interesting to note that the syntax accepted to call generic functions is very particular. It forces you to use this
to refer to the generic function.
For example for a function defined as:
private <T> Object genericFunction(T t){
//function code
}
When referring to it, the following is gives a syntax error:
Object o = <ClassName>genericFunction(ClassName t);
While the following is accepted:
Object o = this.<ClassName>genericFunction(ClassName t);
Why is this so? Shouldn't it take both of them?
Upvotes: 3
Views: 134
Reputation: 280141
It's required by the Java Language Specification.
MethodInvocation:
- MethodName ( [ArgumentList] )
- TypeName . [TypeArguments] Identifier ( [ArgumentList] )
- ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
- Primary . [TypeArguments] Identifier ( [ArgumentList] )
- super . [TypeArguments] Identifier ( [ArgumentList] )
- TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )
The TypeArguments
element always has to come after some expression followed by a .
. It cannot preceded a simple method name.
Upvotes: 6