![](/img/trans.png)
[英]Constructor was only called once when calling static method twice that returns the static object?
[英]Restrict calling static setter method only once
我有以下代碼:
public final class SomeStaticClass {
private static Map<String, Map<String,String>> tMap;
private SomeStaticClass(){
//Private Constructor to avoid instance creation
}
//getter method here to retrieve the map.
public static void setMap(Map<String, Map<String,String>> map){
tMap = map;
}
}
我想限制 setMap 方法只能調用一次,這樣 tMap 以后就不能修改了。 tMap 只會在應用程序啟動期間設置一次,之后會被多個對象訪問。
public static void setMap(Map<String, Map<String,String>> map){
if (null == tMap) // This will make sure tMap initialized only once
tMap = map;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.