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 ...
随机推荐
- iOS UIApplicatin和它的delegate
每一个UIApplication代表一个应运程序,而且UIApplication是个单例类: ios程序一旦启动,创建的第一个对象就是UIApplication对象: // 拿到UIApplica ...
- /lib64/libc.so.6: version `GLIBC_2.14' not found问题
<备忘> 参考文章: https://my.oschina.net/zhangxu0512/blog/262275 问题答疑: http://blog.sina.com.cn/s/blog ...
- Microsoft SQL Server 存储过程举例
-- if SP is existed, drop it. if (object_id('InvHoldToDPS', 'P') is not null) drop proc InvHoldToDPS ...
- python 关键字参数
原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 函数可以通过 关键字参数 的形式来调用,形 ...
- HDU 5781 ATM Mechine 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 ATM Mechine Time Limit: 6000/3000 MS (Java/Othe ...
- linux 操作
正在运行的内核和系统信息 # uname -a # 获取内核版本(和BSD版本) # lsb_release -a # 显示任何 LSB 发行版版本信息 # cat /etc/SuSE-release ...
- Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)
Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...
- 【bzoj1005】[HNOI2008]明明的烦恼
1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4175 Solved: 1660[Submit][Stat ...
- Eclipse Plugin for Hadoop
Eclipse 官网下载向导 下载 下载的安装文件放到~/setupEnv,将安装到/opt目录下 cd ~/setupEnv sudo tar zxvf eclipse-java-kepler-SR ...
- spring mvc Controller与jquery Form表单提交代码demo
1.JSP表单 <% String basePath = request.getScheme() + "://" + request.getServerName() +&qu ...