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中的代码块,静态代码块优先。



你可能感兴趣的:(Java)