Examples of Java internal classes and class loader _java

Source: Internet
Author: User
Tags static class

Inner class

 Class A {//inner1 to be used after a initialization, that is, to be called class Inner1 {int k = 0 by an object of A; static int j = 0; 
    A loaded, Inner1 did not load, so this static variable J can not be used immediately, the error final int z = 0; /*static void Say1 () {}*/void Say2 () {}}//inner2 can use the static class Inner after a has been loaded. 
    2 {int k = 0; 
    static int j = 0; 
    final int z = 0; static void Say1 () {} void Say2 () {}}//Call inner class void C () {final int x = 0;//fin 
     
    When Al is decorated, the new A () can be invoked by a local inner class (). New Inner1 ()//Non-static inner class Inner1, which requires the object of the class it belongs to call new A (). New Inner1 (). Say2 (); 
    A.inner2.say1 (); The new A.inner2 (). Say2 ()//Non-static Method Say2 () requires the object of the class to which it belongs to invoke class Inner3 {void print () {System.out.println ( 
      x); 
    The call to the local inner class in the * * method is required after declaration because, * the order of execution in the method is from top down, and this class * is equivalent to a local variable of course it must be declared first, then use the * 
  New Inner3 (). print (); } 
 
} 

The basic responsibility of the

class loader Java.lang.ClassLoader
Java.lang.ClassLoader class is to find or generate its corresponding byte code based on the name of a specified class. You then define a Java class from these byte codes, an instance of the Java.lang.Class class. In addition, ClassLoader is also responsible for loading resources needed for Java applications, such as image files and configuration files. However, this article only discusses the functionality of its load classes. To complete this responsibility for the load class, ClassLoader provides a series of methods:
GetParent ()   Returns the parent class loader of the class loader.
LoadClass (String name)   Loads a class with the name named, and the result returned is an instance of the Java.lang.Class class.
Findclass (String name)   finds a class with the name named, and the result returned is an instance of the Java.lang.Class class.
Findloadedclass (String name)   Looks up a class that has already been loaded with name, and the result returned is an instance of the Java.lang.Class class.
DefineClass (String name, byte[] B, int off, int len)   Converts the contents of byte array B into Java classes, and the result returned is an instance of the Java.lang.Class class. This method is declared final.
Resolveclass (class<?> c)   link to the specified Java class.

public class Classloadertest extends ClassLoader {public 
  static void Main (string[] args) throws SecurityException, No suchmethodexception { 
     
    //root class loader, loaded core class library 
     url[] urls = Sun.misc.Launcher.getBootstrapClassPath (). Geturls (); 
     for (URL u:urls) { 
     System.out.println (U); 
     } 
    Extended class loader,, Loaded System properties: Java.ext.dirs The path returned by class 
    System.err.println ("Extended Class (extention ClassLoader) loader" 
        + Classloader.getsystemclassloader (). GetParent ())//non-inheritance relationship, only in the sense of 
    System.out.println (System.getproperty (" Java.ext.dirs ")); 
    Application (System application ClassLoader) class loader, loading system environment variable path or CLASSPATH 
    //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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.