Initialization and Class loading - Java
可以说,类的代码在初次使用时才加载。这通常指加载发生于创建类的第一个对象之时,但当访问
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的更多相关文章
- 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] ...
 - Thinking in Java——笔记(7)
		
Reusing Classes The first is composition,You're simply reusing the functionality of the code, not it ...
 - 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 ...
 - Java 6 JVM参数选项大全(中文版)
		
原文来自: http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm 本文是基于最新的SUN官方文档Java SE 6 Hotsp ...
 - 【Java设计模式】单例模式
		
### 1. 概述> 单例模式是确保某一个类中有且只有一个实例. ----------### 2. 饿汉式单例``` javapublic class SingletonInstance { p ...
 - Java theory and practice
		
This content is part of the series: Java theory and practice A brief history of garbage collection A ...
 - [Java] SSH框架笔记_Struts2配置问题
		
1.Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/D:/Progr ...
 - 深入理解JVM—Java 6 JVM参数配置说明
		
原文地址:http://yhjhappy234.blog.163.com/blog/static/316328322011119111014657/ 使用说明< xmlnamespace pre ...
 - 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 ...
 
随机推荐
- Careercup - Microsoft面试题 - 5428361417457664
			
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
 - C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
			
上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 st ...
 - Mysql的主从数据库没有同步的解决办法
			
Mysql的主从数据库没有同步的解决办法 今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. ...
 - c++ dirname() basename()
			
http://linux.about.com/library/cmd/blcmdl3_dirname.htm #include <iostream> #include <libgen ...
 - 【Lua】Lua中__index与元表(转)
			
转载于:http://blog.csdn.net/xocoder/article/details/9028347 Lua的表本质其实是个类似HashMap的东西,其元素是很多的Key-Value对,如 ...
 - 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑
			
倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...
 - API文档管理工具-数据库表结构思考.
			
API文档管理工具-数据库表结构思考. PS: 管理工具只是为了方便自己记录API的一些基本信息,方便不同的开发人员 (App Developer, Restful API Developer)之间的 ...
 - 用npm安装express后express命令找不到
			
Windows 平台加了 npm install -g express 也不行AppData\Roaming\npm 下面没有 express.bat 解决办法: sudo npm install - ...
 - Win32 Plus Extra Height of Caption Bar
			
you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...
 - codeforces 439C  Devu and Partitioning of the Array(烦死人的多情况的模拟)
			
题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...