Examples of Java's inner class and class loader

  • 2020-05-09 18:30:59
  • OfStack

The inner class


class A { 
  //Inner1  Want to be in  A  After the initialization   To be used is to be A Is called by the   
  class Inner1 { 
    int k = 0; 
    // static int j = 0; //A After loading, Inner1 It's not loaded, so this   A static variable j  Unable to use immediately, error report  
    final int z = 0; 
    /*static void say1() { 
 
    }*/ 
    void say2() { 
       
    } 
 
  } 
  //Inner2  in A Once it's loaded, it's ready to use   
  static class Inner2 { 
    int k = 0; 
    static int j = 0; 
    final int z = 0; 
    static void say1() { 
 
    } 
    void say2() { 
       
    } 
  } 
  //  Calling inner class  
  void c() { 
    final int x = 0;// final  After modification, it can be invoked by a local inner class  
    new A().new Inner1();//  Non-static inner class Inner1 , which requires an object invocation of the class to which it belongs  
    new A().new Inner1().say2(); 
     
    A.Inner2.say1(); 
    new A.Inner2().say2();//  Nonstatic method say2() , which requires an object invocation of the class to which it belongs  
 
    class Inner3 { 
      void print() { 
        System.out.println(x); 
      } 
    } 
    /* 
     *  A call to a local inner class in a method needs to be made after the declaration   Because,  
     *  The order of execution in the method is   From the top down, and this class  
     *  Is equivalent to 1 a   local   variable   Of course,   Declare it before you use it  
     */ 
    new Inner3().print(); 
  } 
 
} 

Class loader java.lang.ClassLoader
The basic responsibility of the java.lang.ClassLoader class is to find or generate the corresponding byte code of a specified class based on its name, and then define an Java class from this byte code, i.e. an instance of the java.lang.Class class. In addition, ClassLoader is also responsible for loading the resources required for the Java application, such as image files and configuration files. However, this article only discusses its ability to load classes. To fulfill this responsibility of loading classes, ClassLoader provides a series of 1 methods:
getParent()   returns the parent class loader for the class loader.
loadClass(String name)   loads a class named name and returns an instance of the java.lang.Class class.
findClass(String name)   looks up a class named name and returns an instance of the java.lang.Class class.
findLoadedClass(String name)   looks up a loaded class named name and returns an instance of the java.lang.Class class.
defineClass(String name, byte[] b, int off, int len)   converts the contents of the byte array b into the Java class, returning the result as an instance of the java.lang.Class class. This method is declared as final.
resolveClass(Class < ? > c)   links to the specified Java class.


public class ClassLoaderTest extends ClassLoader { 
  public static void main(String[] args) throws SecurityException, NoSuchMethodException { 
     
    //  The root class loader, which loads the core class libraries  
     URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs(); 
     for (URL u : urls) { 
     System.out.println(u); 
     } 
    //  The extension class   Loader,, loaded system properties: java.ext.dirs  Returns the path below  class 
    System.err.println(" The extension class (extention classLoader) loader " 
        + ClassLoader.getSystemClassLoader().getParent());// Non-inheritance, just in the sense  
    System.out.println(System.getProperty("java.ext.dirs")); 
    //  application ( system  application classLoader) Class loader,   Load system environment variables  PATH  or  CLASSPATH 
    //  The specified JAR Package and classpath  
    System.err.println(" application ( system ) Class loader " + ClassLoader.getSystemClassLoader()); 
    // System.out.println(System.getenv("PATH")); 
    // System.out.println(System.getenv("CLASSPATH")); 
} 
} 


Related articles: