Java class loading and initialization sequence timing

Class loading timing (five kinds)

The virtual machine will trigger the following five cases the class is first loaded

  1. Encountered new, getstatic, setstatic, invokestaticbytecode
  2. Java.lang.reflect package using a method based on the reflecting call
  3. When loading a class, find its parent class has not been loaded, to load the parent class
  4. When the virtual machine starts, you need to specify (with a master class main()class method), the virtual machine to load such
  5. JDK 1.7 dynamic language support, java.lang.invoke.MethodHandleexamples of final analytical result is that REF_getStatic, REF_setStatic, REF_invokeStaticwhen the method handle, will load the corresponding class

Class initialization sequence

  1. Parent class <clinit>code in the block
  2. Subclass <clinit>block code
  3. Parent class <init>code in the block
  4. Subclass <init>block code

clinit block comprising a block of static variables static
init block comprising a member variable with builder

Published 19 original articles · won praise 0 · Views 5751

Guess you like

Origin blog.csdn.net/weixin_46221133/article/details/104221105