I am wondering whether I can set the annotation variable in the runtime?
Asked
Active
Viewed 7,465 times
1 Answers
14
No, that's not possible.
Annotations are stored in the class file as constant values and can't be computed at runtime.
The best you can do is store some kind of "instructions" on how to compute the value.
For example you could store a simple expression in some scripting language and execute that when reading the annotation value or you could specify the name of a method to call to get the real value. One example of such a langauge is the Spring Expression Language (SpEL) used in Spring.

Joachim Sauer
- 302,674
- 57
- 556
- 614
-
@AshokkumarGanesan: "sample code" of an interpreter of a simple expression language? No, that's way out of scope of this question. Use something like Spring EL if you need stuff like this. – Joachim Sauer Sep 16 '19 at 08:46