一、构造器初始化
2019-07-14 本文已影响0人
云烟渐成雨
1.1 初始化顺序
变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍会在任何方法(包括构造器)被调用之前得到初始化
class Window {
Window(int test) {
System.out.println("window(" + test + ")");
}
}
public class InitialValues {
Window w1 = new Window(1);
InitialValues() {
System.out.println("InitialValues()");
w3 = new Window(33);
}
Window w2 = new Window(2);
void print() {
System.out.println("print()");
}
Window w3 = new Window(3);
public static void main(String[] args) {
InitialValues initialValues = new InitialValues();
initialValues.print();
}
}
运行结果如下:
window(1)
window(2)
window(3)
InitialValues()
window(33)
print()
1.2 静态数据的初始化
- 初始化的顺序是先静态对象(如果它们尚未因前面的对象创建过程而被初始化),而后是“非静态”变量。
- 静态初始化只有在必要时刻才会执行。在例子中如果不创建Table对象,也不引用Table.bowl1或Table.bow2,那么静态的Bowl bow1和bow2永远不会被创建。
class Bowl{
Bowl(int test){
System.out.println("Bowl("+test+")");
}
void f1(int test){
System.out.println("f1("+test+")");
}
}
class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int test){
System.out.println("f2("+test+")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
public Cupboard() {
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int test){
System.out.println("f3("+test+")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
System.out.println("Creating new Cupboard in main");
new Cupboard();
System.out.println("Creating new Cupboard in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
运行结果如下:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
1.3 显示的静态初始化
“静态块”如下所示
class Cup{
static int i;
static {
i = 47;
}
}
尽管上面这段代码看起来像一个方法,但实际是一段跟在static关键字后面的代码。与其他静态初始化动作一样,这段代码仅执行一次:当首次生成这个类的一个对象时,或者首次访问属于那个类的静态数据成员时(即便从未生成过那个类的对象)
class Cup {
Cup(int test) {
System.out.println("Cup(" + test + ")");
}
void f(int test) {
System.out.println("f(" + test + ")");
}
}
class Cups{
static Cup cup1;
static Cup cup2;
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cups(){
System.out.println("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.cup1.f(1);
}
// static Cups cups = new Cups();
}
运行结果如下:
Inside main()
Cup(1)
Cup(2)
f(1)
1.4 非静态实例化初始化
从下面的例子可以看到实例初始化子句是在两个构造器之前执行的
class Mug{
Mug(int test){
System.out.println("Mug("+test+")");
}
}
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
}
Mugs(){
System.out.println("Mugs()");
}
Mugs(int test){
System.out.println("Mugs("+test+")");
}
public static void main(String[] args) {
System.out.println("Inside main()");
new Mugs();
System.out.println("new Mugs() completed");
new Mugs(1);
System.out.println("new Mugs(1) completed");
}
}
运行结果如下:
Inside main()
Mug(1)
Mug(2)
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
Mugs(1)
new Mugs(1) completed