title: Dependency injection on class with only static methods?
body: <p>I have a set of API classes which contain only static methods and a single private constructor so they cannot be instantiated. However, I would like a third party developer to be able to modify the behaviour of the API's to suit their needs.</p>
  
  <hr>
  
  <p>This is the solution I have so far (dependency injection via static setter method). This is the public API class that the developer would use. As you can see, it has a dependency on <code>StaticApiImpl</code>.</p>
  
  <pre><code>public class StaticApi {
      private static StaticApiImpl impl = new StaticApiImpl();
  
      private StaticApi() {}
  
      public static void setImpl(StaticApiImpl impl) {
          StaticApi.impl = impl;
      }
  
      public static void doThing() {
          impl.doThing();
      }
  }
  </code></pre>
  
  <p>This is the default API implementation as coded myself.</p>
  
  <pre><code>public class StaticApiImpl {
      public void doThing() {
          System.out.println("Do thing the default way.");
      }
  }
  </code></pre>
  
  <p>This is a hypothetical extended version of the default implementation that a third party might write:</p>
  
  <pre><code>public class MyCustomStaticApiImpl extends StaticApiImpl {
      @Override
      public void doThing() {
          System.out.println("Do thing differently.");
      }
  }
  </code></pre>
  
  <p>The developer would then simply inject their dependency via the setter method upon initialisation of their plugin:</p>
  
  <pre><code>public void onLoad() throws Exception {
      StaticApi.setImpl(new MyCustomStaticApiImpl());
  }
  </code></pre>
  
  <p>My question is: is this the correct way of doing it? Is there perhaps some design pattern dedicated to cases such as this that I have not heard of?</p>