title: Is it possible to switch Class versions at runtime with Java?
body: <p>I have a java project I want to create which will be built on top of some vendor APIs.  The APIs connect to servers running said vendor's software and perform various actions.</p>
  
  <p>I have 2 different versions of the servers supported by 2 different API versions.  Any changes to the API's are in internal implementation only. I.E. The classes, interfaces, methods, etc. available to me in the older version exist in the newer version.  Therefore the code I write should compile and run with either API version.  There is a version number in the API presented to the servers when using the API to connect that prevents you from using a different version API on that particular server.</p>
  
  <ol>
  <li><p>Is there a way to switch JAR files on the fly at runtime? (something like a c/c++ DLL??)</p></li>
  <li><p>If switching API versions at runtime isn't possible, what is the most elegant way to handle the problem.  Build the code 2x (one for each api version)?</p></li>
  </ol>
  
  <p>I hope I'm missing something but approach 2 doesn't seem ideal.  Here's a more concrete example of why:</p>
  
  <pre><code>package org.myhypotheticalwrapper.analyzer;
  
  import org.myhypothetical.worker;
  import org.myhypothetical.comparator;
  
  public class Analyzer {
  
      Worker w1 = new Worker();
      Worker w2 = new Worker();
  
      Comparator c = new Comparator(w1.connectAndDoStuff(),w2.connectAndDoStuff());
      c.generateReport();
  }
  </code></pre>
  
  <p>This is my dilema.  I want w1 to be built with the old API and w2 be built with the new API so they can connect to the appropriate servers.   Other than the API's they sit on top of, they are the same (identical code).   Do I have to create two uniquely named Class types for W1 and W2 even though their code is identical, simply to accommodate different API versions?  It seems like that could get unwieldy fast, if I had many API versions that I wanted to interact with.</p>
  
  <p>Any suggestions and comments greatly appreciated.</p>
  
  <p>-new guy</p>