4
public Class Constants {
    public static final String single = "aabbcc";
    public static final String[] ttt = {"aa", "bb", "cc"};
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
public @interface Anno {
    String aaa() default "aaa"; //this is allowed.
    String bbb() default Constants.single; //this is allowed.
    String[] ccc() default {}; //this is also allowed.
    String[] ddd() default Constants.ttt; //while this is not!
}

as the example shown above, I don't get it why String array constants are not allowed as annotation attribute value?

2
  • I don't believe there's such a thing as an "array constant" in Java... the syntax you give is an "array initializer" with runtime semantics. Commented Jan 23, 2016 at 5:59
  • What is the compiler error message? Commented Jan 23, 2016 at 6:36

1 Answer 1

3

Like Jim Garrison mentioned in a comment, there is no such thing as an "array constant" in Java.

It is easily demonstrated that an array is not a constant:

// Right now, Constants.ttt contains {"aa", "bb", "cc"}
Constants.ttt[1] = "foobar";
// Right now, Constants.ttt contains {"aa", "foobar", "cc"}

So it's not so much that String array constants are not allowed, as that there is no such thing as a String array constant in Java.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.