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>