title: Class Loading and Initializing
body: <p>I am trying to understand the exact scenario when a class get loaded and initialized. I see the two articles below give different answers</p>
  
  <p><a href="http://javarevisited.blogspot.com/2012/07/when-class-loading-initialization-java-example.html" rel="nofollow noreferrer">javarevisited</a>-<em>A Class is <strong>initialized</strong> in Java whenan Instance of class is created using either <strong>new</strong>() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.</em></p>
  
  <p><a href="http://www.javaworld.com/article/2077260/learn-java/learn-java-the-basics-of-java-class-loaders.html" rel="nofollow noreferrer">Java world-</a><em>So when are classes <strong>loaded</strong>? There are exactly two cases: when the new bytecode is executed (for example, FooClass f = <strong>new</strong> FooClass();) and when the bytecodes make a static reference to a class (for example, System.out).</em> </p>
  
  <p>So when I create an instance of class using new keyword is class loaded or initialized?</p>
  
  <hr>
  
  <p>Another thing to ask regarding loading due to reference variable:</p>
  
  <p><a href="http://javarevisited.blogspot.com/2012/07/when-class-loading-initialization-java-example.html" rel="nofollow noreferrer">javarevisited</a>-<em>Class loading is done by Class Loaders in Java which can be implemented to eagerly load a class as soon as another class <strong>references</strong> it or lazy load the class until a need of class initialization occurs</em></p>
  
  <p>What does author mean by <strong>referencing</strong> here? Does he imply if A class have  reference variable of B class then B class get loaded when its reference is encountered in A ?? </p>
  
  <p>But the author below says class NotUsed is not loaded but I see ClassInitializationTest class having its <strong>reference</strong></p>
  
  <p><a href="http://javarevisited.blogspot.com/2012/07/when-class-loading-initialization-java-example.html" rel="nofollow noreferrer">javarevisited</a>-</p>
  
  <p>/**
    * Java class which is not used in this program, consequently not loaded by JVM
    */</p>
  
  <pre><code>class NotUsed {
   static { System.out.println("NotUsed Class is initialized "); }
  </code></pre>
  
  <p>}</p>