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

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. iTween

    http://u3d.as/content/pixelplacement/i-tween/1s9 download http://itween.pixelplacement.com/documenta ...

  2. Careercup - Google面试题 - 5661939564806144

    2014-05-06 01:40 题目链接 原题: Give a N*N matrix, print it out diagonally. Follow up, if it is a M*N matr ...

  3. 【Sum Root to Leaf Numbers】cpp

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  4. File "/struts-tags" not found

    前言 由于在某个jsp引用了struts标签库,导致该错误产生--这是stuts项目算是一道经典错误,往往最后的解决方式是更换Tomcat.今天我记录的是引起这一错误的一个非常隐藏的原因. 错误描述 ...

  5. 设计模式之享元模式(FlyWeight)

    #include <iostream> #include <string> #include <list> #include <vector> usin ...

  6. 【BZOJ】【1012】【JSOI2008】最大数maxnumber

    线段树 ……现在再来看这题感觉好水啊,当年的大老虎现在也变成小花猫了,真是令人感动<_< /************************************************ ...

  7. C#指针与字节数组的操作

    private static byte[] ReadBytesFromPtr(IntPtr intPtr, int bufferLength) { var result = new byte[buff ...

  8. 父页面 调用iframe方法

      父页面调用Iframe的方法 document.getElementById("tabIf0").contentWindow.Search();     Jquery 方式: ...

  9. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  10. HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)

    题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include&l ...