我在自定义注释下方,
@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"
我不想为默认值写一个直接值,但我想从其他类中引用它。 那我该怎么做呢?
答案 0 :(得分:1)
您必须将MAX_DATA_ROW
定义为static
和final
:
public class Constant {
public static final int MAX_DATA_ROW = 1;
}
答案 1 :(得分:1)
您的常量MAX_DATA_ROW必须为“ public static final”,否则不是真正的常量。