Why is it not an error to call a non-parameterized method with type arguments?

I have the following Java program that I did not expect to compile, but it did:

class Test { public static void f() { } void m() { Test.<String>f(); } } 

Why does javac allow calling a non-parameterized method this way?

My java compiler version: javac 1.7.0_75

+6
source share
1 answer

An explicit type parameter is simply ignored.

This is indicated in the JLS section 15.12.2.1 :

  • If the method call contains explicit type arguments, and the element contains a general method, then the number of type arguments is equal to the number of method type parameters.

This section assumes that a non-generic method may be potentially applicable to a call that provides arguments of an explicit type. Indeed, this may be applicable. In this case, the type arguments are simply ignored.

+5
source

Source: https://habr.com/ru/post/982679/


All Articles