How to use constants definded in other class file as the default value in a custom annotation [duplicate]

Multi tool use
How to use constants definded in other class file as the default value in a custom annotation [duplicate]
This question already has an answer here:
I hava below custom annotation,
@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;
}
I got a compile error say:
"Attribute value must be constant"
I don't want to write a direct value to default but I want to refer it from other class.
so how can I accomplish that ?
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
MAX_DATA_ROW
static final
seems it doesn't recognize your value as a constant.
– Stultuske
1 hour ago
2 Answers
2
Your constant MAX_DATA_ROW must be "public static final", otherwise it isn't a real constant.
You must define MAX_DATA_ROW
as static
and final
:
MAX_DATA_ROW
static
final
public class Constant {
public static final int MAX_DATA_ROW = 1;
}
It works when
MAX_DATA_ROW
isstatic final
– Héctor
1 hour ago