我有以下自定义注释,
@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"
我不想直接将值写入默认值,但我想从其他类引用它。 那么我怎样才能做到这一点呢?
最佳答案
您必须将 MAX_DATA_ROW
定义为 static
和 final
:
public class Constant {
public static final int MAX_DATA_ROW = 1;
}
关于java - 如何使用另一个类文件中定义的常量作为自定义注释中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51320883/