0

Why does the second Test in my scenario has the syntax error The value for annotation attribute SuppressWarnings.value must be an array initializer on the SuppressWarnings line?

public class AnnotationTest {
    private static final String supUnused = "unused";
    private static final String supDeprecation = "deprecation";
    private static final String[] suppressArray = { "unused", "deprecation" };

    public static void main(String[] args) {
        // Test 1
        @SuppressWarnings( { supUnused, supDeprecation } )
        int a = new Date().getDay();

        // Test 2
        @SuppressWarnings(suppressArray)    // syntax error
        int b = new Date().getDay();
    }
}

If you're passing the parameters as two single constants, it works.
If you're passing it with an array constant, there is a syntax error.

What is the explanation for this error?

4
  • SuppressWarnings documentation might helps Commented Jan 21, 2014 at 8:25
  • @RafaEl: Thanks for the documentation link! The point is, that I already know, for what you have to use SupressWarnings ;) Commented Jan 21, 2014 at 8:34
  • yes of course I know that you know that. but.. okay, I have no idea why I gave you the link :D Commented Jan 21, 2014 at 8:38
  • @Downvoter: please explain! Commented Jan 31, 2014 at 15:08

1 Answer 1

5

If you're passing it with an array constant, there is a syntax error.

Annotation arguments must be constant.

The suppressArray is declared final, but this only means that you can not reassign the suppressArray variable with another array reference. You can still change the suppressArray's content, e.g.

suppressArray[0] = "someOtherString";

In your first example you use an array initializer inline.

@SuppressWarnings( { supUnused, supDeprecation } )

Therefore no other class can obtain a reference to it and thus can not change the array's content.

At least a look at the JLS 9.7.1 gives a detailed explanation.

The annotation arguements are name value pairs where T is the type of the name value pair while V is the value:

  • If T is a primitive type or String, and V is a constant expression (§15.28).
  • V is not null.
  • If T is Class, or an invocation of Class, and V is a class literal (§15.8.2).
  • If T is an enum type, and V is an enum constant.

and

An ElementValueArrayInitializer is similar to a normal array initializer (§10.6), except that annotations are permitted in place of expressions.

Sign up to request clarification or add additional context in comments.

1 Comment

Great and plausible answer! I was struggling with must be an array initializer. Because, it is in fact an array initializer, but in a different way... And your explanation shows up, why it is not possible to pass an array with a reference. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.