title: Loading a class twice in JVM using different loaders
body: <p>I have one question regarding concepts of class loading. How to load a .class file twice in JVM. I am also writing an excerpt of code that I have written to accomplish this..</p>
  
  <p>1) Loader 1 code</p>
  
  <pre><code>public class MyClassLoader extends ClassLoader {
  
      public MyClassLoader(){
          super(MyClassLoader.class.getClassLoader());
      }
  
      public Class loadClass(String classname){
          try {
              return super.loadClass(classname);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>2) Loader 2 code</p>
  
  <pre><code>public class AnotherClassLoader extends ClassLoader {
  
      public AnotherClassLoader(){
          super(AnotherClassLoader.class.getClassLoader());
      }
  
      public Class loadClass(String classname){
          try {
              return super.loadClass(classname);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>3) Now i am loading a class named A using this two different class loaders. I suppose the operation classA1==newClassA should return false. Here is the code:</p>
  
  <pre><code>public static void main(String[] args) {
          MyClassLoader loader1 = new MyClassLoader();
          AnotherClassLoader newLoader = new AnotherClassLoader();
              System.out.println("Load with Custom Class Loader instance");
              Class classA1 = loader1.loadClass("com.hitesh.coreJava.A");
              System.out.println("Class Loader:::"+classA1.getClassLoader());
              Class newClassA = newLoader.loadClass("com.hitesh.coreJava.A");
              System.out.println("Class Loader:::"+newClassA.getClassLoader());
              System.out.println(classA1==newClassA);
              System.out.println(classA1.hashCode() + " , " + newClassA.hashCode());
  
      }
  </code></pre>
  
  <p>4) Result of executing above code:</p>
  
  <blockquote>
    <p>Load with Custom Class Loader instance
    Class Loader:::sun.misc.Launcher$AppClassLoader@11b86e7
    Class Loader:::sun.misc.Launcher$AppClassLoader@11b86e7
    true
    1641745 , 1641745</p>
  </blockquote>
  
  <p>Could you please explain this</p>