您的位置:首页 > 编程语言

静态成员变量静态代码块和构造方法的执行顺序

2017-11-28 17:56 204 查看

类型一:静态成员变量+非静态成员变量+构造方法

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();
}

class Bowl{

public Bowl(int marker){
System.out.println("Bowl("+marker+")");
}

void f1(int marker){
System.out.println("f1("+marker+")");
}
}

class Table{
static Bowl bowl1 = new Bowl(1);
static Bowl bowl2 = new Bowl(2);

public Table(){
System.out.println("Table()");
bowl2.f1(1);
}

void f2(int marker){
System.out.println("f2("+marker+")");
}
}

class Cupboard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
static Bowl bowl5 = new Bowl(5);

public Cupboard(){
System.out.println("Cupboard()");
bowl4.f1(2);
}

void f3(int marker){
System.out.println("f3("+marker+")");
}
}


**输出结果:

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、要执行main方法首先要加载StaticInitialization这个类。所以先初始化了StaticInitialization里面的两个静态成员变量。

2、当table被初始化的时候打印:Bowl(1) Bowl(2) Table() f1(1);

3、当cupboard 被初始化的时候打印:Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(2)(注意:因为初始化的时候会先加载静态成员变量或者静态代码块,这个要看在类中放的顺序的,优先加载前面的,加载完了静态成员变量再加载非静态成员变量再执行构造方法);

4、执行main方法,因为staic静态成员变量只会初始化一次,所以打印:creating new Cupboard() in main Bowl(3) Cupboard() f1(2) creating new Cupboard() in main Bowl(3) Cupboard() f1(2)。

5、最后打印:f2(1) f3(1)。

类型二:静态成员变量+静态代码块+构造方法

public class StaticInitialization2 {

//static Cups cups1 = new Cups();//(2)
//static Cups cups2 = new Cups();//(2)

public static void main(String[] args) {
System.out.println("Inside main");
Cups.cup1.f(99);//(1)
}
}

class Cup{

public Cup(int marker){
System.out.println("Cup("+marker+")");
}

void f(int marker){
System.out.println("f("+marker+")");
}
}

class Cups{
static Cup cup1;
static Cup cup2;
static Cup cup3 = new Cup(3);
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}

public Cups(){
System.out.println("Cups()");
}

}


**输出结果:

Inside main

Cup(3)

Cup(1)

Cup(2)

f(99)

**

总结:先执行main方法,打印Inside main;

当一个类被首次new创建出来或者首次访问这个类的静态成员变量或者调用静态方法时才会初始化,而且只创建一次,不同点是new出来会调用构造方法,而用类名点属性或者方法的方式不会调用构造方法;

从这里可以看出静态成员变量和静态代码块的执行顺序无优先级关系,只和在类里面的位置有关;

当吧(2)中的注释放开

**输出结果:

Cup(3)

Cup(1)

Cup(2)

Cups()

Cups()

Inside main

f(99)**

总结:说明静态成员变量只会初始化一次,new出来的构造方法会执行多次;

类型三:非静态实例初始化

public class Mugs {

Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
System.out.println("mug1 和 mug2初始化了");
}

public Mugs(){
System.out.println("Mugs()");
}

public Mugs(int i){
System.out.println("Mugs(int)");
}

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");
}
}

class Mug{
public Mug(int marker) {
System.out.println("Mug("+marker+")");
}
void f(int marker){
System.out.println("f1("+marker+")");
}
}


输出结果:

**Inside main()

Mug(1)

Mug(2)

mug1 和 mug2初始化了

Mugs()

new Mugs() completed

Mug(1)

Mug(2)

mug1 和 mug2初始化了

Mugs(int)

new Mugs(1) completed**

总结:1、先是执行main方法,打印:Inside main()

2、new Mugs(),这时候打印:Mug(1) Mug(2) mug1 和 mug2初始化了 Mugs()

3、在打印:new Mugs() completed

4、new Mugs(2),打印:Mug(1) Mug(2) mug1 和 mug2初始化了 Mugs(int)

5、最后执行:new Mugs(1) completed

其实可以把

{

mug1 = new Mug(1);

mug2 = new Mug(2);

System.out.println(“mug1 和 mug2初始化了”);

}

当做一个普通的成员变量来执行,只不过有一些特殊,每次创建都会执行,也没有优先级;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: