`
come_for_dream
  • 浏览: 116578 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

JVM类加载之深入探究(一)

    博客分类:
  • JVM
阅读更多

JVM类加载之深入探究

      本博客参考自http://1509221674.iteye.com/blog/2158485。

     我们平时在Eclipse或者IDEA写一个java程序的时候,都是编辑器帮助我们自动完成编译,我们点击绿色按钮运行程序,整个过程遂心应手,但是你可曾想过你写的代码如何被编译?编译后的字节码又如何在运行的时候被加载到JVM(JAVA Virtual Machine)?

     我们写的java代码要被JVM执行首先要被编译成.class的文件,这个文件是可以被JVM执行的字节码,也就是相当于1000111这种被JVM所识别的”机器语言“。那么我们的被编译好的class文件在运行的时候又是怎么被加载到JVM中呢?下面就结合一个例子进行说明。

     首先看第一个例子:

class Shape {
	private int ShapeCommonValue = init("the ShapeCommonValue is inited");
	private static int ShapeStaticValue = init("the ShapeStaticValue is inited");
	static {
		init("the static block is executed");
	}

	public Shape() {
		init("the construction of Shape is executed");
	}

	public static int init(String out) {
		System.out.println(out + "  the staticValue is  " + ShapeStaticValue);
		ShapeStaticValue++;
		return ShapeStaticValue;
	}
}

public class Circle extends Shape {
	private static int CircleStaticValue = init("the CircleStaticValue is inited");
	private int CircleCommonValue = init("the CircleCommonValue is inited");

	public Circle() {
		init("the construction of Circle is executed");
	}

	public static void main(String[] args) {
		Circle circle = new Circle();
	}
}

    这个例子说明了子类和父类的初始化的过程,

        首先JVM对Circle类进行加载时,发现有父类Shape

        则JVM首先加载父类Shape 的.class文件 

 

        然后 对父类中静态资源进行初始化:先加载静态变量,后执行静态块

 

        对子类的静态资源初始化

 

        对父类Shap普通属性初始化

 

        对父类构造方法的初始化

 

        对子类成员属性的初始化

 

        对子类构造方法的初始化

 

则运行程序后将验证以上过程:



 

 

  

  • 大小: 207.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics