可以说,类的代码在初次使用时才加载。这通常指加载发生于创建类的第一个对象之时,但当访问

static域或static方法时,也会发生加载(通过下面的这段代码验证)。

class LoadTest {
// The static clause is executed upon class loading:
static {
System.out.println("Loading LoadTest");
}
static void staticMember() {}
static int i = 0;
}
public class classLoading {
public static void main(String args[]) {
System.out.println("Calling static member");
//new LoadTest(); // 1
//LoadTest.staticMember(); //
LoadTest.i++; //
System.out.println("Creating an object");
}
}

  Output:

lxw@:::~/eclipse/java/javaComLine$ java classLoading
Calling static member
Loading LoadTest
Creating an object

  无论保留语句1,语句2还是语句3,代码运行结果都是上面的结果。

  所有的static对象和static代码段会在加载时按照程序中的顺序(定义类时的书写顺序)而依次初始化。

  本文将以下面的代码为例展开论述:

class Insect {
private int i = 9;
protected int j;
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 =
printInit("static Insect.x1 initialized");
static int printInit(String s) {
System.out.println(s);
return 47;
}
} public class Beetle extends Insect {
private int k = printInit("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static int x2 =
printInit("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
//Beetle b = new Beetle(); // 1
//Beetle b; //
}
}

Output:

lxw@:::~/eclipse/java/javaComLine$ java Beetle
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor

Analyze:

  Part 1.

  上面的代码执行时,首先将试图访问main(),于是开始启动并找出Beetle类的编译代码,在加载的过程中

编译器注意到它有一个基类,于是先对基类进行加载。此时基类中的static初始化开始被执行,然后执行导出

类中的static初始化。”先执行基类然后再执行导出类“的原因是导出类的static初始化可能会依赖于基类成员是

否被初始化。

  简略的说就是:基类的static数据成员初始化 -> 导出类的static数据成员初始化 -> main()

  至此,不管代码中是否创建一个基类或导出类的对象都执行这些步骤。

  Part 2.

  更改上面的代码,将语句2的注释去掉,则运行结果与上面的结果相同。

  Part3.

  更改上面的代码,将语句1的注释去掉:

  Output:  

lxw@:::~/eclipse/java/javaComLine$ java Beetle
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = , j =
Beetle.k initialized
k =
j =

  请注意各个输出的顺序。当用new创建导出类的一个对象时,其执行顺序如下:

  1. 基类初始化:先是基类的非静态成员初始化,然后是基类构造器的执行

  2. 导出类初始化:先是导出类的非静态成员初始化,然后是导出类构造器的执行

Initialization and Class loading - Java的更多相关文章

  1. Error loading: \Java\jdk1.6.0_35\jre\bin\server\jvm.dll

    先看看错误:complie: [exec] Error loading: D:\Program Files\Java\jdk1.6.0_35\jre\bin\server\jvm.dll [exec] ...

  2. Thinking in Java——笔记(7)

    Reusing Classes The first is composition,You're simply reusing the functionality of the code, not it ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Reusing Classes

    The trick is to use the classes without soiling the existing code. 1. composition--simply create obj ...

  4. Java 6 JVM参数选项大全(中文版)

    原文来自: http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm 本文是基于最新的SUN官方文档Java SE 6 Hotsp ...

  5. 【Java设计模式】单例模式

    ### 1. 概述> 单例模式是确保某一个类中有且只有一个实例. ----------### 2. 饿汉式单例``` javapublic class SingletonInstance { p ...

  6. Java theory and practice

    This content is part of the series: Java theory and practice A brief history of garbage collection A ...

  7. [Java] SSH框架笔记_Struts2配置问题

    1.Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/D:/Progr ...

  8. 深入理解JVM—Java 6 JVM参数配置说明

    原文地址:http://yhjhappy234.blog.163.com/blog/static/316328322011119111014657/ 使用说明< xmlnamespace pre ...

  9. Java learning notes (1):Basic Knowlege points

    Basic Knowlege points: 1: it's necessary that there is only one public class in per .java file 2: .j ...

随机推荐

  1. EF:Invalid column name 'Discriminator'.

    错误信息: InnerException: System.Data.SqlClient.SqlExceptionHResult=-2146232060Message=Invalid column na ...

  2. BZOJ2039 [2009国家集训队]employ人员雇佣

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2039 鉴于一开始看题如果不仔细是看不懂题目的,还是说一下题目大意 [题目大意]:给定n个人 ...

  3. ios 缓存相关信息收集

    链接:http://www.cnblogs.com/pengyingh/category/353093.html 使用NSURLCache让本地数据来代替远程UIWebView请求 摘要: 原文作者: ...

  4. ```````````````辐射度 Radiometry

    solid angel --立体角 单位 sr  球面度 dω就是对solid angel的微分 4π代表一个球 我发现dω就是对半径为1的球的表面积的微分 所以4π代表一个球  这就是球的表面积.. ...

  5. while小问题

    while(!m_SMque.pop(data)); 看到这个有点忘了,如果pop返回false会一直执行pop,其实这个执行的是空语句,而while每次执行都需要判断条件,所以如果pop返回fals ...

  6. js获得浏览器页面高宽

    不同的浏览器可能会有一些差别,使用的时候请先进行测试. var s = ""; s += " 网页可见区域宽:"+ document.body.clientWi ...

  7. [百度空间] [原]跨平台编程注意事项(二): windows下 x86到x64的移植

    之前转的: 将程序移植到64位Windows 还有自己乱写的一篇: 跨平台编程注意事项(一) 之前对于x64平台的移植都是纸上谈兵,算是前期准备工作, 但起码在写代码时,已经非常注意了.所以现在移植起 ...

  8. Using Hooks

    The following code examples demonstrate how to perform the following tasks associated with hooks: In ...

  9. Windows Live Writer安装失败错误解决方案

    初次使用博客园.看到官方推荐Windows Live Writer来写作博客.看着也不错.可以离线写作.不用担心浏览器突然卡卡卡,未响应的危险. Windows Live Writer是博客园推荐博客 ...

  10. ZendStudio导入一个已有的网站

    解决方法:新建'PHP Project',选择'Create project at existiong location(from existing source)',路径指向你的网站根目录.