title: How does Java find already loaded classes?
body: <p>I know that Java uses the ClassLoader hierarchy for loading the classes.</p>
  
  <p>For example a program:</p>
  
  <pre><code>public void test(){
      A a = new A(); // Line 1 The class is accessed first time here so it should be loaded and defined
  
      A ab = new A(); //Line 2 How can the second line be represented?
  }
  </code></pre>
  
  <p>The first line of the code is similar to </p>
  
  <pre><code>Thread.currentThread().getContextClassLoader().loadClass("A");
  </code></pre>
  
  <p>So the class is loaded and defined to create instance of <code>Class</code>.</p>
  
  <p>Now the question is when the second line is executed the <code>Class A</code> is referred again, will Java not lookup for the class again and return the same loaded instance of the Class?</p>
  
  <p>As the Java classloader document says that every class loader should maintain the instances of loaded classes and return the same instances for the next call.</p>
  
  <p>Where does Java keep the loaded classes? <code>ClassLoader</code> class has a <code>Vector</code> of classes which is called by VM to add the loaded classes.</p>
  
  <p>Maybe the question is a bit confusing, basically I am trying to figure out from which method are the already loaded classes returned. I tried to keep a debug point in the <code>loadClass()</code> method but it is not called for the <code>Line 2</code>. </p>
  
  <p>The <code>loadClass()</code> method of <code>ClassLoader</code> has <code>findLoadedClass</code> method but that too is not called.</p>