Java Reflection - Java Class.getClassLoader()


Class.getClassLoader() has the following syntax.

public ClassLoader getClassLoader()


In the following code shows how to use Class.getClassLoader() method.

/*from www .ja  va 2s  .  co m*/

public class Main {

  public static void main(String[] args) throws Exception {
    Class cls = Class.forName("java.lang.String");
    // returns the ClassLoader object associated with this Class.
    ClassLoader cLoader = cls.getClassLoader();

    if (cLoader == null) {
      System.out.println("The default system class was used.");
    } else {
      // returns the class loader
      Class loaderClass = cLoader.getClass();

The code above generates the following result.