Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The value for annotation attribute Min.value must be a constant expression

Tags:

java

Eclipse keeps giving me the error:

The value for annotation attribute Min.value must be a constant expression

But I am most definitely giving the annotation a constant.

private static final int MIN_YEAR = Calendar.getInstance().get(Calendar.YEAR) - 1;

@Min(MIN_YEAR)

If I change it to be

private static final int MIN_YEAR = 2013;

It's perfectly happy, but I shouldn't have to do that. Does anyone know why or how my MIN_YEAR constant isn't considered a constant if it's declared with an evaluated expression instead of a plain number?

like image 570
CorayThan Avatar asked Jan 14 '23 07:01

CorayThan


1 Answers

The expression

private static final int MIN_YEAR = Calendar.getInstance().get(Calendar.YEAR) - 1;

will be determined only in run-time, but

private static final int MIN_YEAR = 2013;

is determined in compilation time, so it's allowed since the values in the annotations should be resolved at compilation time and not runtime.

like image 147
Maroun Avatar answered Feb 02 '23 00:02

Maroun