![](/img/trans.png)
[英]Convert compile-time-constant int to compile-time-constant String in Java
[英]Initializing a static final field in static initializer does not give me a compile-time-constant (Java)
我嘗試在Servlet中使用注釋@MultipartConfig(location = Test.MY_CONST)。
當我從下面使用代碼1時,效果很好。 當我從下面使用代碼2時,在編譯時會得到以下輸出:
錯誤:屬性值必須恆定
@MultipartConfig(位置= Test.MY_CONST)
代碼1
public class Test {
public static final String MY_CONST = "/constant/path";
}
代碼2
public class Test {
public static final String MY_CONST;
static {
MY_CONST = "/constant/path";
}
}
問題:我有幾個問題可以歸結為“ 這是怎么回事? ”
MY_CONST
分配一個編譯時間常數,並不會使MY_CONST
成為一個編譯時間常數,所以我會收到此錯誤嗎? System.out.println(Test.MY_CONST)
打印什么,如果我通過反射更改MY_CONST
的值? 您不能更改“最終”變量。 “常量”變量不能更改。 它也直接初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.