1. 程式人生 > >Java 中被static 修飾的屬性,方法和程式碼塊在什麼時候載入

Java 中被static 修飾的屬性,方法和程式碼塊在什麼時候載入

intellij社群版+jdk.7

package com.static_life;
import com.SVS2;
/**
 * Created by Administrator on 2016/12/21.
 */
public class StaticVariableStart {

    public static int  age = 124;
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String [] args){


        SVS2 svs2;//breakpoint
        svs2= new SVS2();//
        String s = SVS2.string;

        StaticVariableStart staticVariableStart = new StaticVariableStart();



        System.out.print(SVS2.class.getGenericSuperclass());

    }
}
在主方法中設定多個斷點
package com;

/**
 * Created by Administrator on 2016/12/21.
 */
public class SVS2 {

    public static String string = "";

    {
        System.out.println("這是程式碼塊");
    }

    static {


        System.out.println("這是SVS2 的靜態程式碼塊");
    }


}

breakpoint1:SVS2.string = Class not load:com.SVS2;靜態池中已經存在StaticVariableLife.age = 124.(No executable code)

breakpoint2:SVS2.string = "";SVS2載入,取到值,並且 自動執行了SVS2中的程式碼塊,靜態程式碼塊優先。