在Java中为什么会出现这个错误:'属性值必须是常量'? [英] In Java why this error: 'attribute value must be constant'?

查看:301
本文介绍了在Java中为什么会出现这个错误:'属性值必须是常量'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些TestNG代码,我传递一个名为 timeOut = TESTNG_TEST_TIMEOUT 的测试注释参数。

I have some TestNG code, where I am passing a Test annotation parameter called timeOut = TESTNG_TEST_TIMEOUT .

@Test(description = "Tests something.", groups = { "regression" }, 
   timeOut = TESTNG_TEST_TIMEOUT, enabled = true)

在我的TestBase课程中,我有这个成员:

And in my TestBase class I have this member:

public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);

当我使用上面的代码行时,我得到'属性值必须是常量'错误Eclipse。

When I use the above line of code, I get a 'attribute value must be constant' error in Eclipse.

但是,如果我只是这样定义成员,它就可以工作:

But, if I simply define the member like so, it works:

public final static long TESTNG_TEST_TIMEOUT = 300000;

TimeUnit的使用不是常数吗?

Is the use of TimeUnit not a constant?

推荐答案

public final static long TESTNG_TEST_TIMEOUT = 300000;

常量变量,一种常量表达式

public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);

不是。

注释成员期望常量表达式(以及其他一些内容,如枚举和 Class 文字)。

Annotation members expect constant expressions (and a few other things like enums and Class literals).

这篇关于在Java中为什么会出现这个错误:'属性值必须是常量'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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