简单说说

:在初始化时执行,从上到下的收集有初始值的静态成员域或static块的值,进行赋值,执行本类的函数时,父类的函数已经执行完毕,若本类不具有静态成员域或static块,则JVM不会为该类产生函数。

:其实就是构造函数,在生成class文件时,编译器会在构造函数中添加一些代码,在本类的构造函数中,会最优先调用父类的函数,接着执行剩余构造函数中剩余的代码,我们来看看下面的代码:

class deal0 
{
  
}
class deal extends deal0
{
    int n;
    int m=2;
    deal()
    {
       n=1;
    }
}
public class Try {
    public static void main(String[] args) {
       deal x=new deal();
    } 
}

反编译deal.class文件:

  Last modified 2018-1-31; size 240 bytes
  MD5 checksum 8b1b2a8d7345905b383f50f5fd6d2a2a
  Compiled from "Try.java"
class deal extends deal0
  minor version: 0
  major version: 52
  flags: ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#15         // deal0."":()V
   #2 = Fieldref           #4.#16         // deal.m:I
   #3 = Fieldref           #4.#17         // deal.n:I
   #4 = Class              #18            // deal
   #5 = Class              #19            // deal0
   #6 = Utf8               n
   #7 = Utf8               I
   #8 = Utf8               m
   #9 = Utf8               
  #10 = Utf8               ()V
  #11 = Utf8               Code
  #12 = Utf8               LineNumberTable
  #13 = Utf8               SourceFile
  #14 = Utf8               Try.java
  #15 = NameAndType        #9:#10         // "":()V
  #16 = NameAndType        #8:#7          // m:I
  #17 = NameAndType        #6:#7          // n:I
  #18 = Utf8               deal
  #19 = Utf8               deal0
{
  int n;
    descriptor: I
    flags:

  int m;
    descriptor: I
    flags:

  deal();
    descriptor: ()V
    flags:
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0                           //将this指针压入操作数栈
         1: invokespecial #1                  //Method deal0."":()V,此处调用父类的构造函数,父类构造函数的执行结果写入到this指针所指的内存处
         4: aload_0                           //将this指针压入操作数栈
         5: iconst_2                          //将常量池中2压入操作数栈
         6: putfield      #2                  // Field m:I,使内存中m的值为2,此时会用到操作数栈中的this指针,来找到实例对象在堆内存中的位置。
         9: aload_0                           //将this指针压入操作数栈
        10: iconst_1                          //将常量池中的1压入操作数栈,this指针作用同上
        11: putfield      #3                  // Field n:I,给内存中的n赋予1
        14: return
      LineNumberTable:
        line 10: 0
        line 8: 4
        line 11: 9
        line 12: 14
}
SourceFile: "Try.java"
构造函数的字节码解释请看注释,可以看到先调用父类的函数,接着初始化具有初始值的变量(实测后,初始化顺序为代码中由上到下的出现顺序),然后在执行我们自己写的代码。

你可能感兴趣的:(JVM)