1. Load class

Class loader

1. Load class

In Java code, the type of the load, and the connection initialization process are in a program is running completed.

  • Type: This refers to Class class, such as a User object, and the type herein refers User.class, rather than out of the new User object.
  • Load: will get the specified class file, or remote url file, then loaded into memory
  • Connection: to determine the accuracy of the reference relationship between class and class, verification and inspection class files. Here it is divided into three stages
  • Initialization: the loaded class static variable assignment correct initialization process

  • During the program run: mean load, connection, complete the initialization process is done in Runtime runtime, not static compile time.

1.1 class loading - The connection process is divided into three stages

  1. Verification: verify the correctness class file, though most of the class files are compiled by javac come, but the compiled class files can again be modified text editor manually.
  2. Preparation: to allocate memory for the type of static variables and assign default values initialized

    Note example: public static int a = 1; for the variables a, at this stage, will be allocated to a memory, and initializes the default values.
    Here does not resolve to a = 1, where the default values will be determined according to the data type, i.e. int - 0, double - 0.0, boolean - false, object - null, etc. As the operation is performed a = 1 , follow-up is done in the initialization phase.

  3. Analysis: The class reference into a direct reference symbol

1.2 class several ways to load

Refers to the class loader to load the class files into the binary data memory, and drops it into the method area , and allocating memory to create a java.lang.Class objects (JVM specification does not require where to put Class object, which put the HotSpot into the zone method), used to package the class data structure in the process area, that is regardless of how many instances of the object class has, there will be only a class object of reasons.

Class way to load:

  1. Local class file, such as class files under the classpath
  2. Through the network to download class files
  3. Extract class files from the zip, jar package archive, etc.
  4. Load class files from a proprietary database
  5. Will compile the source file into a class file, such as dynamic agents, and aop section

1.3 summary points

  1. For static variables, only directly defines a class static variable fields, will be initialized
  2. When a class is initialized, the parent class must be the first to be completed initialization

2. The class loader

jvm loaded class file, parses the class parameter memory, this process depends on the class loader to achieve.

3. lead to the end jvm virtual machine lifecycle four reasons

  1. Manually call System.exit ()
  2. Normal program execution ends
  3. Program encounters an exception, resulting in termination of the program
  4. Operating system abnormalities, leading to stop jvm

4. Use of active and passive use of the class

java class or interface as long as the first initiative to use the situation will initialize them

4.1 actively used seven kinds of scenes

  1. Instantiate an object
  2. Call the class static variable, set a static variable assignment
  3. Static method call class
  4. Using reflection-based processing, for example, to instantiate objects Class.forName
  5. For you inherit parent-child relationship, while examples of subclasses, is bound to take the initiative to use its parent class
  6. Jvm virtual machine has been identified as a class start classes, such as execution of the current main class
  7. For after jdk1.7, the results support the dynamic process of language to call the corresponding class is not initialized, the initialization

4.2 passive use

Not actively used are passive use, features passive use is not initialized to lead the class.

Guess you like

Origin www.cnblogs.com/duguxiaobiao/p/12091931.html