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

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. Ruby 多线程探索实践与归纳总结

    Ruby 多线程 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程. 线程是程序中一个单一的顺序控制流程,在单个程序中同时运行多个线程完成不同的工作,称为多线程. Ruby 中我们可以通 ...

  2. selenium IDE--录制和回放脚本

    1 selenium IDE--录制脚本 准备工作:firefox 浏览器安装了selenium IDE 插件 实例:打开百度搜索“软件测试” firefox浏览器打开网址:https://www.b ...

  3. c++ swap 函数

    转载地址 1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T ...

  4. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  5. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  6. Leetcode#146 LRU Cache

    原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...

  7. 关于“无法定位程序输入点gzdirect于动态链接库zlib1.dll”的问题

    费劲N多力气编译通过之后,最后启动程序过程中却突然得到“无法定位程序输入点gzdirect于动态链接库zlib1.dll”的问题, 分析究其原因是定位不到zlib1.dll,都知道,程序在找dll的时 ...

  8. Native App执行JS

    iOS: - (void)webViewDidFinishLoad:(UIWebView *)webView{        //js方法名+参数    NSString* jsCode = [NSS ...

  9. C#与C++之间类型的对应{转}

    Windows Data Type   .NET Data Type BOOL, BOOLEAN   Boolean or Int32 BSTR    String BYTE    Byte CHAR ...

  10. HDU 4493 Tutor(精度处理)

    题目 #include<stdio.h> int main() { int t; double a,s; scanf("%d",&t); while(t--) ...