class loading是一个比较复杂的过程。一般说来,类是在其static member被访问时被加载的。在加载时会做的一件事是:初始化static member和static代码段(static block, i.e. static {......}),当然,static是只会被执行only once的。
以下是一个例子(adapted from Chapter 7, Thinking in Java, Fourth Edition):
//@file Beetle.java
import static java.lang.System.out;
class Insect
{
private int i = 1;
protected int j;
private static int x1 = printInit("static Insect.x1 initialized");
Insect()
{
out.println("Insect constructor");
out.println("i = " + i + ", j = " + j + ", x1 = " + x1);
this.j = 2;
}
static int printInit(String s)
{
out.println(s);
return 3;
}
}
public class Beetle extends Insect
{
private int k = printInit("Beetle.k initialized");
private static int x2 = printInit("static Beetle.x2 initialized");
public Beetle()
{
out.println("Beetle constructor");
out.println("j = " + j + ", k = " + k + ", x2 = " + x2);
}
public static void main(String[] args)
{
Beetle b = new Beetle();
}
}
//output:
/*
static Insect.x1 initialized
static Beetle.x2 initialized
Insect constructor
i = 1, j = 0, x1 = 3
Beetle.k initialized
Beetle constructor
j = 2, k = 3, x2 = 3
*/
->首先,访问Beetle.main(),是个static,好,加载Beetle.class;
-->发现Beetle extends Insect,好,加载Insect.class(如果Insect还有base class,则持续这一过程,直到找到root base class);
---->初始化private static int x1( = 3),打印"static Insect.x1 initialized";
---->Insect.class加载完毕
-->继续加载Beetle.class;
---->初始化private static int x2( = 3),打印"static Beetle.x2 initialized";
---->Beetle.class加载完毕,
->开始执行Beetle.main();
-->要new一个Beetle,须先new一个Insect;
---->在执行Insect constructor前,先要初始化member,由于private static int x1是static且已经初始化了,所以这次只初始化private int i( = 1)和protected int j( = 0 by default);
---->执行Insect constructor,打印"Insect constructor"和"i = 1, j = 0, x1 = 3",然后j = 2;
---->Insect constructor执行完毕,即new Insect过程完毕
-->继续new Beetle;
---->同理,在执行Beetle constructor之前,要先初始化member,这里是初始化private int k( = 3),打印"Beetle.k initialized";
---->执行Beetle constructor,打印"Beetle constructor"和"j = 2, k = 3, x2 = 3";
---->Beetle constructor执行完毕,即new Beetle过程完毕
->Beetle.main()执行完毕。
(2009年03月27日补充:more details see [#0x000B])
(2009年09月04日归纳:[#0x0023])
分享到:
相关推荐
INIT: MOV R0 , #0x00 MOV R1 , #0x0FF MAIN: MOV P3 , #0FFH JNB KEY_P33 , readKeyValue ;若有按键按下则跳转到 readKeyValue SJMP MAIN readKeyValue: CALL DELAY10MS ;调用延时函数,消抖 JNB KEY_P33 , ...
The results are: x wafer: 0x001B y wafer: 0x800D n wafer: 0x00 lot n: 0x00312C28706431 and: x wafer: 0x001C y wafer: 0x002A n wafer: 0x00 lot n: 0x002C2870643129 is ''y wafer 0x800D'' a sign-abs ...
oracle 11g 数据文件头block 1解析 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ##powered by :黄林杰_Huanglinjie ##version : 2023-v11 ##联系方式:17767151782 ...KCVCPSCN_KSCNBAS: 0x0010a3b9,ckp scn:1
0X0E中学算术前置知识0X0D数学库函数正文有了三角函数、反三角函数、对数函数、求幂运算,中学阶段涉及的所有具体的数值计算基本就都可以求解。求此表达式的值:第
0X2B循环for前置知识0X29再谈循环正文Lua除了while和repeat语句还提供了一种for循环语句.while和repeat语句循环终止条件可以是任
OV2643规格书开发代码详情HS[15:8] = 0x20HS[7:0] = 0x21vertical start [0x22, 0x23]VS[15:8] = 0x22VS[7:0] = 0x23horizontal width [0x24, 0x25]HW[11:4] = 0x24HW[3:0] = 0x25[7:4]HPAD[3:0] = 0x25[3:0] x 2...
诊断会话控制:0x10 ECU复位:0x11 清除诊断信息:0x14 读取故障码信息:0x19 通过ID读数据:0x22 安全访问:0x27 通讯控制:0x28 通过ID写数据:0x2E 例行程序控制:0x31 请求下载:0x34 数据传输:0x36 退出传输:...
目录图例Stage1 入门Stage2 基础Stage3 字符串处理Stage4 理解函数Stage5 表结构Stage6 流程控制Stage7 模块Stage
1. 度量脚本执行时间 2. 记录时间 3. 计算时间间隔
域的定义如果交换环还额外满足以下条件,则称为域:乘法单位元和加法单位元不相等(1不等于0)。由于存在乘法逆元,就意味着可以进行“除法”,所以有的书上也把域称作
bcdUSB: 0x0110 bDeviceClass: 0xFF bDeviceSubClass: 0x00 bDeviceProtocol: 0xFF bMaxPacketSize0: 0x20 (32) idVendor: 0x064F (Xsh-Systems AG) idProduct: 0x0BD8 bcdDevice: 0x0106 iManufacturer: 0x01 ...
安全运营相关文章本篇整理一些安全运营相关的高质量文章[2] 体系化的WAF安全运营实践, 安全研究与实践,
qt 出现 DirectShowPlayerService::doRender: Unresolved error code 0x80040266 ()
NULL 博文链接:https://aoyi.iteye.com/blog/659179
NULL 博文链接:https://aoyi.iteye.com/blog/631929
OTP : origin = 0x3D7800, length = 0x000800 FLASHE : origin = 0x3E8000, length = 0x004000 FLASHD : origin = 0x3EC000, length = 0x004000 FLASHC : origin = 0x3F0000, length = 0x004000 FLASHB : ...
在运行KalmanFilter的时候需要用到...main.cpp:(.text+0x1eb): undefined reference to cv::KalmanFilter::KalmanFilter(int, int, int, int)' main.cpp:(.text+0x245): undefined reference tocv::Mat::zeros(int
NULL 博文链接:https://aoyi.iteye.com/blog/723571
UWORLD:0x9B99F1F 引擎::世界::等级:0x138 引擎::世界::持久级别:0x30 引擎:: GameInstance :: LocalPlayers:0x38 引擎::世界::拥有游戏实例:0x180 引擎::控制器::控制旋转:0x288 引擎:: Player...
AID: 0xA0:0x0:0x0:0x3:0x96:0x4D:0x34:0x4D:0x30 Version: 2.2 Set of documents referenced in this package: 昜VC Creation] - MIFARE Virtual Card Creation v1.0.2 昜VC Management] - MIFARE Virtual Card...