简体   繁体   English

限制只调用一次 static setter 方法

[英]Restrict calling static setter method only once

I have the below code:我有以下代码:

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;
}
}

I want to restrict the setMap method to be called only once,so that the tMap cannot be modified later.我想限制 setMap 方法只能调用一次,这样 tMap 以后就不能修改了。 The tMap will be set only once during application startup and will be access by multiple objects later. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM