vendredi 22 janvier 2016

What does a static declaration with no return or input value do? [duplicate]

This question already has an answer here:

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:

http://ift.tt/1mlJlq3

Thank you.

Aucun commentaire:

Enregistrer un commentaire