title: Java ClassLoader change
body: <p>I have some class <code>A</code>:</p>
  
  <pre><code>public class A {
      public A(String str) {
          System.out.println("Create A instance: " + str);
      }
  
      public void methodA() {
          System.out.println("#methodA1()");
      }
  }
  </code></pre>
  
  <p>And my class loader implementation:</p>
  
  <pre><code>public class MyClassLoader extends ClassLoader {
      public MyClassLoader() {    
          super();
      }
  
      @Override
      public synchronized Class&lt;?&gt; loadClass(String name) 
              throws ClassNotFoundException {
  
          System.out.println("Load: " + name);
  
          return super.loadClass(name);
      }
  }
  </code></pre>
  
  <p>And now I try to change default class loader in current thread:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class ChangeLoaderTest {
      public static void main(String[] args) {
          // Save class loader so that we can restore later.
          ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
  
          MyClassLoader newLoader = new MyClassLoader();
          try {
              // Set new classloader.
              Thread.currentThread().setContextClassLoader(newLoader);
  
              // My class.
              A a = new A("1");
              a.methodA();
  
              // Standard Java class.
              List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
              list.add(2);
              list.add(3);
          } finally {
              // Restore.
              Thread.currentThread().setContextClassLoader(oldLoader);
          }
      }
  }
  </code></pre>
  
  <p>And <code>ChangeLoaderTest</code> output:</p>
  
  <pre><code>Create A instance: 1
  #methodA1()
  </code></pre>
  
  <p>No one</p>
  
  <pre><code>Load: ...
  </code></pre>
  
  <p>Why? How I can change <code>ClassLoader</code> into some thread?</p>