天天看點

Java - Error: Attribute value must be constant

Error: Attribute value must be constant

  本文位址:http://blog.csdn.net/caroline_wendy   Attribute value must be constant. 屬性的值必須要是Constant.  如,在 Annotation的接口中的值,但是字元串數組不能指定為Constant,Java中 static final。 隻能把純字元設定為 Constant,是以隻能按如下使用:

public @interface SampleAnnotation {
        String[] sampleValues();
    }

    public class Values {
        public static final String v1 = "A";
        public static final String v2 = "B";

        @SampleAnnotation(sampleValues = { v1, v2 })
        public void foo() {
        }
    }
           

數組無法進行常量化,隻能使用字元常量。   參考:  http://stackoverflow.com/questions/2065937/how-to-supply-value-to-an-annotation-from-a-constant-java