[]-Depth understanding of the Java virtual machine initialization class

Class initialization process

Loading class .pngfile

  • load

    The Class file is loaded into memory in binary form

  • verification

    Class check whether the file security, have been properly modified, etc.

  • ready

    Application memory for class variables, set the default value (the default value to initialize variables, such as int is initialized to 0, reference is initialized to null) but have not reached before the initialization class initialized to the true value.

Zero value .pngfile

  • Resolve

    The symbolic reference is converted to a direct reference

  • initialization

    Collected and perform static code block, and Execution methods, Static and static variables of code blocks

  • use

    Application memory for the new object, as an example of the default settings of the variables, is an example of an object the correct initial values ​​are set; generating method

  • Uninstall

    At run time plus the virtual machine parameters +XX:+TraceClassLoadingcan see detailed information on class loading, unloading like to see the same information, you can use-XX:TraceClassUnloading

Active and passive references cited

Active references

  • Encounter new, getstatic, putstatic, invokestatic bytecode instructions of time, if not initialized, initialization progressive
  • When reflective, such as: System.load ( "xxxx.xxxx.xx");
  • Initialize a class, but the parent of this class is not initialized when (initialization of an interface does not require the parent interface to all initialization)
  • Master JVM class to be executed
  • When experiencing dynamic language support

Passive reference

  • By reference to the parent class's subclasses 静态变量or 静态方法, and not initialize the parent class. Reference a static properties of the parent class by the child class that represents the active use of the parent, rather than take the initiative to use subclasses
  • By constructing an array of type, such is not initialized
  • Always direct reference to the type of a class of the time, and not the initialization

Sample Code

class SuperClass {
  public static String msg = "Hello,World";

  static {
    System.out.println("SuperClass.static initializer");
  }
}

class SubClass extends SuperClass {

  public static String msg2 = "Hello,World";

  static {
    System.out.println("SubClass.static initializer");
  }
}
  • Verify not initialize subclass constant reference by the parent class subclasses
   // 验证通过子类引用父类的常量并不会初始化子类
    System.out.println(SubClass.msg);
  • Verify initialize the subclass same time will initialize the parent class
    System.out.println(SubClass.msg2);
  • Initial validation type of the array is not initialized when the type

For array types, which types are dynamically generated during operation JVM, type [Lxxxx.xxxx.xxxx.xxxx.SubClasss; parent class Object, the same way as the two-dimensional array type [[Lxxx.xxx.xxx.SubClass; Object type parent;

    SubClass[] subClasses = new SubClass[1];

Reference constant pool

  • Source
public class ReferenceExample002 {

  public static void main(String[] args) {
    //    实际运行的是 System.out.println(Hello, World);
    // 对于只有运行期才能确定的值,仍然会初始化类
    System.out.println(ExampleClass.msg);
  }
}

class ExampleClass {

  public static final String msg = "Hello, World";

  static {
    System.out.println("ExampleClass.static initializer");
  }
}
  • Information decompiled code removed a direct reference to the ExampleClass

public class ReferenceExample002 {
    public ReferenceExample002() {
    }

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
  • Mnemonic information decompilation ReferenceExample002 are as follows:

Decompile command such as: javap -c xxx.xxx.xxx

public class com.zhoutao.example.ReferenceExample002 {
  public com.zhoutao.example.ReferenceExample002();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #4                  // String Hello, World
       5: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

Class uncertain where constant values ​​during compilation will be initialized

package com.zhoutao.classload;

import java.util.UUID;

public class ReferenceExample003 {

  public static void main(String[] args) {

    System.out.println(ExampleClass.uuid);
  }

  static class ExampleClass {
   
    // 此处引用的并非真正的常量值
    public static final String uuid = UUID.randomUUID().toString();

    static {
      System.out.println("ExampleClass.static initializer");
    }
  }
}

ExampleClass static code block will be executed: uuid value can not be determined and the compiler, it will still initialize a corresponding class, and attention constant value determined during compilation distinguish.

Interface constants

When an interface initialization time, the parent does not require interfaces to complete the initialization, class initialization time and request the parent class is initialized, and the time required to initialize the class parent class initialization is completed

public class ReferenceExample005 {

  public static void main(String[] args) {
    System.out.println(SubInterface.b);
  }

  static interface ParentInterface {
    public static int a = 1;
  }

  static interface SubInterface extends ParentInterface {
    public static int b = 2;
  }
}

JVM parent as an interface and sub-interface or not initialization of this class is initialized, it just使用该接口的静态变量的时候才会进行初始化

The method of the initialization process

Java will be generated for each class Method for static variables will give birth method

public class ReferenceExample006 {

  public static void main(String[] args) {
    ExampleClass exampleClass = ExampleClass.getInstance();
    System.out.println("a = " + ExampleClass.a);
    System.out.println("b = " + ExampleClass.b);
  }

  static class ExampleClass {
    public static int a;

    public static int b = 0;

    private static ExampleClass exampleClass = new ExampleClass();

    private ExampleClass() {
      a++;
      b++;
    }

    public static ExampleClass getInstance() {
      return exampleClass;
    }
  }
}

For the above codes, you can easily know which output value is:

a = 1
b = 1

If after the defined ExampleClass b ExampleClass placed private constructor method, then the output value will be:

a = 1
b = 0

This is because Definition and collect static block when the method is performed in accordance with the order, when in the private constructor, b will be assigned the value of 1, then in the next step public static int b = 0; again will be defined as a b

file

This article from the blog a mass text multiple tools such as operational platform OpenWrite release

Guess you like

Origin www.cnblogs.com/zhoutao825638/p/12388864.html