[英]Final static variable initialized in static block using method that throws exception
[英]How to suppress serial warning for a static block where a final static variable is initialized and manipulated?
我有一個
public final static java.util.Map<String, Type> VAR;
static {
VAR = new java.util.HashMap<>();
VAR.put("a", new com.google.common.reflect.TypeToken<List<Integer>>(){}.getType());
}
凡失蹤serialVersionUID
在TypeToken
(的guava
18.0)導致[serial]
警告,我想取消,因為它沒有用處。 我想避免將@SuppressWarnings("serial")
到類中,因為它太寬了。 靜態塊和聲明都不接受@SuppressWarnings
批注。 我無法將初始化移動到靜態方法,因為其他常量可能尚未初始化(編譯器錯誤)。
我正在使用Java 1.7。
這是一種可能性:
public final static Map<String, Type> VAR;
private static Type listOfIntegerType()
{
@SuppressWarnings("serial")
TypeToken<List<Integer>> t = new TypeToken<List<Integer>>(){};
return t.getType();
}
static {
VAR = new HashMap<>();
VAR.put("a", listOfIntegerType());
}
不要使用靜態塊:
@SuppressWarnings("serial")
public final static java.util.List VAR = new java.util.LinkedList() {
...
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.