Java - 为什么不允许数组常量作为Annotation属性值?

时间:2016-01-23 05:57:09

标签: java annotations

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!
}

如上所示,我不明白为什么不允许String数组常量作为注释属性值?

1 个答案:

答案 0 :(得分:1)

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

很容易证明数组不是常数:

@Entity
@Table(name="AGENTE")
public class Agente {   

    ...

    @JoinColumn(name="AGN_IDAZN", referencedColumnName="AZN_ID")
    @ManyToOne(cascade=CascadeType.PERSIST)
    private Azienda azienda;

    ...
 }

因此,不允许使用String数组常量,因为Java中没有String数组常量。