在 Java 中为什么会出现此错误:“属性值必须是常量”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28031784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:29:03  来源:igfitidea点击:

In Java why this error: 'attribute value must be constant'?

javaannotationstestngconstants

提问by djangofan

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

我有一些 TestNG 代码,我在其中传递了一个名为timeOut = TESTNG_TEST_TIMEOUT.

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

And in my TestBase class I have this member:

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

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

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

当我使用上面的代码行时,我在 Eclipse 中收到“属性值必须是常量”错误。

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

但是,如果我只是像这样定义成员,它会起作用:

public final static long TESTNG_TEST_TIMEOUT = 300000;

Is the use of TimeUnit not a constant?

TimeUnit 的使用不是常数吗?

采纳答案by Sotirios Delimanolis

This

这个

public final static long TESTNG_TEST_TIMEOUT = 300000;

is a constant variable, a type of constant expression.

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

This

这个

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

is not.

不是。

Annotation members expectconstant expressions (and a few other things like enums and Classliterals).

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