java初始化过程和代码先后顺序没有关系,执行顺序依次为:

静态成员变量;

静态域;

成员变量;

构造方法;

注:有基类,先执行基类,后执行子类。


package com.init.order;


public class Fruit {

public Fruit(){

System.out.println("Fruit init function");

}

public  int apple_2 = intPrintln(2);

public static int apple_1 = intPrintln(1);

static{

System.out.println("static area");

}

static int intPrintln(int i){

System.out.println(i);

return 100;

}

}



package com.init.order;


public class Apple extends Fruit{

public Apple(){

System.out.println("apple init function");

}

static int apple = intPrintln(20);

}



package com.init.order;


public class TestMain {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new Apple();

}

}

执行结果:

1

static area

20

2

Fruit init function

apple init function