Java – why should the value of an annotation attribute be a constant expression?
I have the following code
@UIUnitTimeout(8*60*1000) // works @UIUnitTimeout(TimeUnit.MINUTES.toMillis(8)) // does not work
I know that according to JLS, only constant expressions are allowed as the value of annotation attributes But why? Why is there not enough data type matching? If the expression is evaluated at run time, is there anything wrong? Does each specification have logical reasoning?
Solution
Comments are similar to type extensions or metadata about the type
Because Java is a statically typed language (meaning that the type is known at compile time), annotation attribute data (metadata) is also known at compile time - you are defining / declaring data about annotations (extensions)
As a purely practical point, for the annotation processing of the compile time (optional) step, you must know the attribute data at compile time – you have not reached the runtime environment, but you need the attribute data