the @SuppressWarnings("unchecked")
is applied to the sphere of action of declaration and assignment immediately after. This can be assigned to a scope or to a variable by setting a variable.
In the first example, it is applied to a local variable. In the second example, you are trying to apply it when assigning a field that has already been declared.
See that this also does not compile:
public class Test<T> { public void a(int initial_capacity) { T[] backing_array; @SuppressWarnings("unchecked") backing_array = (T[]) new Object[initial_capacity]; } }
and this does not affect the warnings:
public class Test<T> { public void a(int initial_capacity) { @SuppressWarnings("unchecked") T[] backing_array; backing_array = (T[]) new Object[initial_capacity]; } }
In short, SuppressWarnings cannot be applied to a variable in its entirety. It was applied to assignment + declaration (for variables) or throughout the method area when applied to the method.
source share