/**/
/*
*初始化全过程:
*
*1, 第一次创建MyClass类的对象,或者第一次访问MyClass的static方法或字段时,Java解释器会搜寻classpath,找到MyClass.class。
*2, 装载MyClass.class后,会对所有的static数据进行初始化。这样第一个装载Class对象的时候,会先进行static成员的初始化。
*3, 使用new MyClass()创建新对象的时候,MyClass对象的构建进程会先在堆里为对象分配足够的内存。 *
*4, 清零这块新内存,把MyClass对象的primitive类型的成员赋上缺省值。
*5, 执行定义成员数据时所作的初始化。
*6, 执行构造函数。
*/
import static net.mindview.util.Print. * ;
public class Beetle extends Insect
... {
private int k = printInit("Beetle.k initialized");
public Beetle()
...{
print("k = " + k);
print("j = " + j);
}
private static int x2 = printInit("static Beetle.x2 initialized");
public static void main(String[] args)
...{
print("Beetle constructor");
Beetle b = new Beetle();
}
}
class Insect
... {
private int i = 9;
protected int j;
Insect()
...{
print("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 = printInit("static Insect.x1 initialized");
static int printInit(String s)
...{
print(s);
return 47;
}
}
/**/ /* Output:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
*/ // :~
/**/ /****************************************************/
// 变量初始化先后顺序的示例
import static net.mindview.util.Print. * ;
// 当创建Window的实例对象时会有消息提示
class Window
... {
Window(int marker)
...{
print("Window(" + marker + ")");
}
}
class House
... {
Window w1 = new Window(1); // 构造函数前的变量
House()
...{
//构造函数里面的变量
print("House()");
w3 = new Window(33); // 重新赋值w3
}
Window w2 = new Window(2); // 构造函数后的变量
void f()
...{
print("f()");
}
Window w3 = new Window(3); // 结束类体时的对象
}
public class OrderOfInitialization
... {
public static void main(String[] args)
...{
House h = new House();
h.f();
}
}
/**/ /*
* 输出结果: Window(1) Window(2) Window(3) House() Window(33) f()
*
* 从结果看出,虽然域变量w2,w3排在构造函数后面,但它的输出却在构造函数前面
*/
/**/ /****************************************************/
// 数组的初始化
import java.util. * ;
public class ArrayInit
... {
public static void main(String[] args)
...{
//直接赋值方式,局限在于数组在大小编译确定
Integer[] a = ...{
new Integer(1),
new Integer(2),
3, // 自动包装
};
//new方式,适于参数数量未知的场合,或者参数类型未知的场合
Integer[] b = new Integer[] ...{
new Integer(1),
new Integer(2),
3, // 自动包装
};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
}
/**/ /* 输出结果:
[1, 2, 3]
[1, 2, 3]
*/ // :~
*初始化全过程:
*
*1, 第一次创建MyClass类的对象,或者第一次访问MyClass的static方法或字段时,Java解释器会搜寻classpath,找到MyClass.class。
*2, 装载MyClass.class后,会对所有的static数据进行初始化。这样第一个装载Class对象的时候,会先进行static成员的初始化。
*3, 使用new MyClass()创建新对象的时候,MyClass对象的构建进程会先在堆里为对象分配足够的内存。 *
*4, 清零这块新内存,把MyClass对象的primitive类型的成员赋上缺省值。
*5, 执行定义成员数据时所作的初始化。
*6, 执行构造函数。
*/
import static net.mindview.util.Print. * ;
public class Beetle extends Insect
... {
private int k = printInit("Beetle.k initialized");
public Beetle()
...{
print("k = " + k);
print("j = " + j);
}
private static int x2 = printInit("static Beetle.x2 initialized");
public static void main(String[] args)
...{
print("Beetle constructor");
Beetle b = new Beetle();
}
}
class Insect
... {
private int i = 9;
protected int j;
Insect()
...{
print("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 = printInit("static Insect.x1 initialized");
static int printInit(String s)
...{
print(s);
return 47;
}
}
/**/ /* Output:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
*/ // :~
/**/ /****************************************************/
// 变量初始化先后顺序的示例
import static net.mindview.util.Print. * ;
// 当创建Window的实例对象时会有消息提示
class Window
... {
Window(int marker)
...{
print("Window(" + marker + ")");
}
}
class House
... {
Window w1 = new Window(1); // 构造函数前的变量
House()
...{
//构造函数里面的变量
print("House()");
w3 = new Window(33); // 重新赋值w3
}
Window w2 = new Window(2); // 构造函数后的变量
void f()
...{
print("f()");
}
Window w3 = new Window(3); // 结束类体时的对象
}
public class OrderOfInitialization
... {
public static void main(String[] args)
...{
House h = new House();
h.f();
}
}
/**/ /*
* 输出结果: Window(1) Window(2) Window(3) House() Window(33) f()
*
* 从结果看出,虽然域变量w2,w3排在构造函数后面,但它的输出却在构造函数前面
*/
/**/ /****************************************************/
// 数组的初始化
import java.util. * ;
public class ArrayInit
... {
public static void main(String[] args)
...{
//直接赋值方式,局限在于数组在大小编译确定
Integer[] a = ...{
new Integer(1),
new Integer(2),
3, // 自动包装
};
//new方式,适于参数数量未知的场合,或者参数类型未知的场合
Integer[] b = new Integer[] ...{
new Integer(1),
new Integer(2),
3, // 自动包装
};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
}
/**/ /* 输出结果:
[1, 2, 3]
[1, 2, 3]
*/ // :~