2

When I try to run the following program I am getting following warning which puts me in dilemma.

Unused type arguments for the non generic method m() of type TEST class; it should not be parameterized with arguments

void m2() {
    this.<String>m(); // warning here
}

int m() {
  return 1;
}

Now my question is if this not legal then why there is no compilation error ?

T-Bag
  • 10,916
  • 3
  • 54
  • 118

2 Answers2

0

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12

MethodInvocation:
MethodName ( [ArgumentList] )
TypeName . [TypeArguments] Identifier ( [ArgumentList] )
ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
Primary . [TypeArguments] Identifier ( [ArgumentList] )
super . [TypeArguments] Identifier ( [ArgumentList] )
TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )
ArgumentList:
Expression {, Expression}

According to java specification your code is valid, it syntatically matches the ExpressionName . [TypeArguments] Identifier ( [ArgumentList] ).

Warning is probably generated by eclipse as a marker for potential place, which can behave strangely. I think it could be connected with matching potential method to a reference, however I can't give you an example.

staszko032
  • 802
  • 6
  • 16
0

The code is syntactically correct. The warning is shown because, you're not using any type parameter for the method m() but trying to access it like one in line this.<String>m(); You can define the method with a type parameter to fix the warning. I've added a sample piece of code, which will compile & run without warnings.

public class Test {
    void m2() {
        System.out.println(this.<String>m()); // Just printing the return value
    }

    <T> int m() {
      return 1;
    }

    public static void main(String[] args) {
        new Test2().m2();
    }
}

Note: I used a type parameter T for the method, m(). You cannot use any existing class as type parameter(String, Integer, etc.. & Classes defined by you). Since, this will cause another warning like, The type parameter <TypeParameter> is hiding the type <TypeParameter>

Sridhar
  • 1,518
  • 14
  • 27