注释属性Min.value的值必须是一个常量表达式 [英] The value for annotation attribute Min.value must be a constant expression

查看:90
本文介绍了注释属性Min.value的值必须是一个常量表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse不断给我错误:

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)

如果我将其更改为

private static final int MIN_YEAR = 2013;

很高兴,但我不必这样做.有人知道为什么如果我的MIN_YEAR常量是用评估表达式而不是纯数字声明的,为什么我的MIN_YEAR常量不被视为常量?

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?

推荐答案

表达式

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

仅在运行时确定,但

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.

这篇关于注释属性Min.value的值必须是一个常量表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆