简体   繁体   中英

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. The tMap will be set only once during application startup and will be access by multiple objects later.

public static void setMap(Map<String, Map<String,String>> map){
    
if (null == tMap) // This will make sure tMap initialized only once
    tMap = map;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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