2016-01-23 57 views
1
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! 
} 

如上例所示,我不明白为什么字符串数组常量不允许作为注释属性值?Java - 为什么数组常量不允许作为Annotation属性值?

+0

我不相信Java中有一个“数组常量”这个东西......你给出的语法是一个带有运行时语义的“数组初始值设定器”。 –

+0

什么是编译器错误信息? –

回答

0

就像Jim Garrison在评论中提到的那样,在Java中没有像“数组常量”那样的东西。

这是很容易证明,一个数组是不是一个常数:

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

因此,这与其说是String数组常量是不允许的,因为有在Java中的String数组常量没有这样的事。

相关问题