初始化顺序

1 对于继承来讲,初始化次序大致如下:

1)父类的静态变量和静态块。父类的静态变量和静态块的初始化次序是按代码次序执行。

2)子类的静态变量和静态块。子类的静态变量和静态块的初始化次序同父类。

3)父类的非静态变量和非静态块。他们之间初始化次序按代码次序执行。此时如果对象中所有的非静态变量和非静态块没有直接赋值,将执行默认的初始化。(其中非静态变量包括基本类型的变量和对象的引用)

4)父类的构造函数。调用构造函数时,会对实例变量进行初始化。

注意:1), 2)无论类是否产生对象,他们都回执行初始化;3),4)产生对象后才会执行。

5)子类的非静态变量和非静态块。他们之间初始化次序按代码次序执行。

6)子类的构造函数。参考4)



class Baseclass{
// 父类静态变量
private static int x1 =
printInit("父类静态变量");

// 父类 静态块
static{
printInit("父类 静态块");
}


// 父类非静态变量
private int i = printInit("父类非静态变量");

// 父类非静态块
{
print("父类非静态块");
}
// 父类构造函数
Baseclass(){
print("父类构造函数");
i = 39;
}

static int printInit(String s){
print(s);
return 47;
}

// Print with a newLine:
static void print(Object obj){
System.out.println(obj);
}

}
public class Subclass extends Baseclass{
// 子类静态变量
private static int x2 =
printInit("子类静态变量");

// 子类 静态块
static{
printInit("子类 静态块");
}


// 子类非静态变量
private int j = printInit("子类非静态变量");

// 子类非静态块
{
print("子类非静态块");
}
// 子类构造函数
Subclass(){
print("子类构造函数");
j = 39;
}
public static void main(String[] args) {
Subclass s = new Subclass();
}
}






class Baseclass{
// 父类静态变量
private static int x1 =
printInit("父类静态变量");

// 父类 静态块
static{
printInit("父类 静态块");
}


// 父类非静态变量
private int i = printInit("父类非静态变量");

// 父类非静态块
{
print("父类非静态块");
}
// 父类构造函数
Baseclass(){
print("父类构造函数");
i = 39;
}

static int printInit(String s){
print(s);
return 47;
}

// Print with a newLine:
static void print(Object obj){
System.out.println(obj);
}

}
public class Subclass extends Baseclass{
// 子类静态变量
private static int x2 =
printInit("子类静态变量");

// 子类 静态块
static{
printInit("子类 静态块");
}


// 子类非静态变量
private int j = printInit("子类非静态变量");

// 子类非静态块
{
print("子类非静态块");
}
// 子类构造函数
Subclass(){
print("子类构造函数");
j = 39;
}
public static void main(String[] args) {
// Subclass s = new Subclass();
}
}





注销了最后一行,其结果:符合上面所说的注意事项

父类静态变量
父类 静态块
子类静态变量
子类 静态块



------------------------------------------------------------------------------

其实,初始化顺序也不一定是上面所说的那样,关键是代码怎么组织。但是大致是走上面所说的这个过程(也要遵守)

public class Test {

private static Test test = new Test();//已经用到构造函数,就调用构造函数初//始化实例变量,反而调构造函数比静态初始化块先进行.

public int num1;

public int num2 = 2;

static {
System.out.println(test.num1);
System.out.println(test.num2);
}

// private Test() {
// num1++;
// num2++;
// }

// public static Test getInstance() {
// return test;
// }

public static void main(String[] args) {
// Test test = Test.getInstance();
// System.out.println(test.num1);
// System.out.println(test.num2);
}
}


上面的代码出自:
http://hi.baidu.com/splendor518/blog/item/4228058b1bffaed9fc1f10da.html

---------------------------------------------------------------------------

package intial;
class Parent {
private int i = 9;
protected int j;
private static int x1 = printInit("static Parent.x1 initialized");

Parent() {
print("i = " + i + ", j = " + j);
j = 39;
}

protected static int printInit(String s) {
print(s);
return 47;
}

/**
* @param string
*/
protected static void print(String string) {
System.out.println(string);
}
}

public class Child extends Parent {

private int k = printInit("Child.k initialized");
private int y=printj();
private static int x2 = printInit("static Child.x2 initialized");

public Child() {
print("k = " + k);
print("j = " + j);
print("y = " + y);
}

private int printj() {
// TODO Auto-generated method stub
System.out.println("y="+y);//y先初始化为默认值0;
return 3;
}

public static void main(String[] args) {
print("Child constructor");
Child b = new Child();
}
}



http://andy-leung.iteye.com/blog/819077
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值