Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the <code>ClassLoader.findLoadedClass()</code> method. If it returns null, then the class isn't loaded. This way you don't load the class if it wasn't already loaded.</p> <hr> <p><strong>WARNING</strong> : This code doesn't really work here, in the system ClassLoader, <code>findLoadedClass()</code> is protected, you need to override it with your own ClassLoader.</p> <p><em>Check the link below <strong>On the same topic</strong> to check if a class is loaded with the system ClassLoader</em></p> <pre><code>if(ClassLoader.getSystemClassLoader().findLoadedClass("java.lang.String") != null){ System.out.println("Yepee, String is loaded !"); } </code></pre> <hr> <p>Very good point from @irreputable :</p> <blockquote> <p>"loaded" doesn't mean "initialized". initialization only happens at precise moments defined by JLS3 $12.4.1 </p> </blockquote> <p>And I quote :</p> <blockquote> <p>A class or interface type <em>T</em> will be initialized immediately before the first occurrence of any one of the following:</p> <ul> <li><em>T</em> is a class and an instance of <em>T</em> is created.</li> <li><em>T</em> is a class and a static method declared by <em>T</em> is invoked.</li> <li>A static field declared by <em>T</em> is assigned.</li> <li>A static field declared by <em>T</em> is used and the field is not a constant variable <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#10931" rel="nofollow noreferrer">(§4.12.4)</a>.</li> <li><em>T</em> is a top-level class, and an <code>assert</code> statement <a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#35518" rel="nofollow noreferrer">(§14.10)</a> lexically nested within <em>T</em> is executed.</li> </ul> <p>Invocation of certain reflective methods in class <code>Class</code> and in package <code>java.lang.reflect</code> also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.</p> </blockquote> <hr> <p><strong>Resources :</strong></p> <ul> <li><a href="http://download.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#findLoadedClass(java.lang.String)" rel="nofollow noreferrer">Javadoc - <code>ClassLoader.findLoadedClass()</code></a></li> <li><a href="http://onjava.com/pub/a/onjava/2005/01/26/classloading.html" rel="nofollow noreferrer">Internals of Java Class Loading</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.1" rel="nofollow noreferrer">JLS - §12.4.1 When Initialization Occurs</a></li> </ul> <p><strong>On the same topic :</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/482633/in-java-is-it-possible-to-know-whether-a-class-has-already-been-loaded">In Java, is it possible to know whether a class has already been loaded?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload