title: How to use guava class to instance map in interface?
body: <p>I have interface called RestClientInterface which is implemented by abstract RestClient class, those class is extended by SearchRestClient and IndexRestClient. Both of those classes are singletons. I would like to be able implement in the interface static method getInstance. I decided to use Guava library like suggested here: <a href="https://stackoverflow.com/questions/22458838/how-can-i-implement-abstract-static-methods-in-java">How can I implement abstract static methods in Java?</a>.</p>
  
  <p>I have implemented two statics methods in interface:</p>
  
  <pre><code>public interface RestClientInterface {
  
      ClassToInstanceMap&lt;RestClientInterface&gt; classToInstanceMap = MutableClassToInstanceMap.create();
  
      static &lt;T extends RestClientInterface&gt; T getInstance(Class&lt;T&gt; type) {
          return classToInstanceMap.getInstance(type);
      }
  
      static &lt;T extends RestClientInterface&gt; void registerInstance(Class&lt;T&gt; type, T identity) {
          classToInstanceMap.putIfAbsent(type, identity);
      }
  }
  </code></pre>
  
  <p>And next registered instances in both extending classes:</p>
  
  <pre><code>public class IndexRestClient extends RestClient {
  
      static {
          RestClientInterface.registerInstance(IndexRestClient.class, getInstance());
      }
  
      /**
       * IndexRestClient singleton.
       */
      private static IndexRestClient instance = null;
  
      private IndexRestClient() {}
  
      /**
       * Return singleton variable of type IndexRestClient.
       *
       * @return unique instance of IndexRestClient
       */
      private static IndexRestClient getInstance() {
          if (Objects.equals(instance, null)) {
              synchronized (IndexRestClient.class) {
                  if (Objects.equals(instance, null)) {
                      instance = new IndexRestClient();
                      instance.initiateClient();
                  }
              }
          }
          return instance;
     } 
  }
  </code></pre>
  
  <p>Next I call this like that:</p>
  
  <pre><code>IndexRestClient restClient = RestClientInterface.getInstance(IndexRestClient.class);
  </code></pre>
  
  <p>But every time I get null. Static registration of the instances doesn't work as the array of registered instances is empty. How can I correctly instantiate those both classes?</p>