如何使用在其他类文件中定义的常量作为自定义注释中的默认值

时间:2018-07-13 08:29:35

标签: java java-8

我在自定义注释下方,

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {

    String value() default "";

    // use constants value defined in other file
    int capacity() default com.constant.Constant.MAX_DATA_ROW;
}

我遇到一个编译错误:

"Attribute value must be constant"

我不想为默认值写一个直接值,但我想从其他类中引用它。 那我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

您必须将MAX_DATA_ROW定义为staticfinal

public class Constant {

    public static final int MAX_DATA_ROW = 1;

}

答案 1 :(得分:1)

您的常量MAX_DATA_ROW必须为“ public static final”,否则不是真正的常量。