This question already has an answer here:
- Static initializer in Java 8 answers
I was looking at the following Service Locator Pattern description and noticed a class with the following. What does this mean? Does this mean whenever this class is instantiated there will be one copy of it? Is this code automatically executed upon instantiation of the ServiceLocator object?
static {
cache = new Cache();
}
The entire class is here:
public class ServiceLocator {
private static Cache cache;
static {
cache = new Cache();
}
public static Service getService(String jndiName){
Service service = cache.getService(jndiName);
if(service != null){
return service;
}
InitialContext context = new InitialContext();
Service service1 = (Service)context.lookup(jndiName);
cache.addService(service1);
return service1;
}
}
The full pattern is here for reference:
Thank you.
Aucun commentaire:
Enregistrer un commentaire