user11171
user11171

Reputation: 4071

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

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

class Test {
    public static void f() {
    }

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

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

My Java compiler version is: javac 1.7.0_75

Upvotes: 6

Views: 883

Answers (1)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62884

The explicit type parameter is simply ignored.

This is stated in JLS, Section 15.12.2.1:

  • If the method invocation includes explicit type arguments, and the member is a generic method, then the number of type arguments is equal to the number of type parameters of the method.

This clause implies that a non-generic method may be potentially applicable to an invocation that supplies explicit type arguments. Indeed, it may turn out to be applicable. In such a case, the type arguments will simply be ignored.

Upvotes: 5

Related Questions