java初始化的加载顺序-----我的理解

来源:互联网 发布:数组结束标志 编辑:程序博客网 时间:2024/06/07 00:16

在继承的时候,java初始化时加载的顺序是先加载完各个类的所有的静态函数,静态变量(这两个平等关系,那个在前初始化那个先);剩下的在每个类中依次进行:无名函数<==>变量(这两个平等关系,那个在前初始化那个先)-------->构造函数。静态的函数和变量是跨类执行的,不是静态的就先执行完基类再依次到下一个类。

例子如下:

package test;


class Apple{
public Apple(String str){
System.out.println("Apple("+str+")");
if("静态Son after".equals(str)){System.out.println("--------------------------静态方法全部初始化完毕");}
}
}


class GrandFather{
static Apple apple2 = new Apple("静态GrandFather before");
Apple apple = new Apple("GrandFather before");
{ System.out.println("GrandFather无名函数before");}
static{
System.out.println("GrandFather静态before");
};
public GrandFather(){
System.out.println("GrandFather构造函数 ");
System.out.println("--------------------------GrandFather初始化完毕");
};
static{
System.out.println("GrandFather静态after");
}
{ System.out.println("GrandFather无名函数after");}
Apple apple1 = new Apple("GrandFather after");
static Apple apple3 = new Apple("静态GrandFather after");
}


class Father extends GrandFather{
static Apple apple2 = new Apple("静态Father before");
Apple apple = new Apple("Father before");
{ System.out.println("Father无名函数before");}
static{
System.out.println("Father静态before");
};
public Father(){
System.out.println("Father构造函数 ");
System.out.println("--------------------------Father初始化完毕");
};
static{
System.out.println("Father静态after");
}
{ System.out.println("Father无名函数after");}
Apple apple1 = new Apple("Father before");
static Apple apple3 = new Apple("静态Father after");
}


public class Son extends Father{
static Apple apple2 = new Apple("静态Son before");
Apple apple = new Apple("Son before");
{ System.out.println("Son无名函数before");}
static{
System.out.println("Son静态函数before");
}
public Son(){
System.out.println("Son构造函数");
System.out.println("--------------------------Son初始化完毕");
};
static{
System.out.println("Son静态函数after");
}
{ System.out.println("Son无名函数after");}
Apple apple1 = new Apple("Son before");
static Apple apple3 = new Apple("静态Son after");
public static void main(String[] args){
new Son();
}
}

//output

Apple(静态GrandFather before)
GrandFather静态before
GrandFather静态after
Apple(静态GrandFather after)
Apple(静态Father before)
Father静态before
Father静态after
Apple(静态Father after)
Apple(静态Son before)
Son静态函数before
Son静态函数after
Apple(静态Son after)
--------------------------静态方法全部初始化完毕
Apple(GrandFather before)
GrandFather无名函数before
GrandFather无名函数after
Apple(GrandFather after)
GrandFather构造函数 
--------------------------GrandFather初始化完毕
Apple(Father before)
Father无名函数before
Father无名函数after
Apple(Father before)
Father构造函数 
--------------------------Father初始化完毕
Apple(Son before)
Son无名函数before
Son无名函数after
Apple(Son before)
Son构造函数
--------------------------Son初始化完毕


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 评书杨家将刘兰芳播讲全集 刘兰芳评书大全在线听 刘兰芳评书呼家将93回 呼家将评书刘兰芳全集 刘兰芳评书网赵匡胤 呼家将刘兰芳评书全集 刘兰芳评书赵匡胤演义 刘兰芳评书网在线收听 刘兰芳赵匡胤演义100回 刘兰芳评书在线收听 刘兰芳评书赵匡胤演义100 刘兰芳岳飞传全集mp3在线听 刘兰芳评书大全岳飞传 评书赵匡胤刘兰芳播讲 扬家将评书刘兰芳播讲 岳飞传刘兰芳全集117 刘兰芳评书杨家将93回在线听 评书岳飞传刘兰芳播讲117回 刘兰芳评书赵匡胤演义100回 刘兰芳评书杨家将全集 刘兰芳评书杨家将全传 刘兰芳评书岳飞全传 刘兰芳评书岳飞传大全117集 刘兰芳评书网赵匡胤演义 刘兰芳平书杨家将全集 刘兰芳评书呼家将全集 评书杨家将刘兰芳广播全集 评书网刘兰芳岳飞传 评书杨家将刘兰芳mp3下载 评书岳飞传刘兰芳全集 刘兰芳岳飞传全集mp3下载 评书网刘兰芳杨家将 刘兰芳的评书岳飞传 刘兰芳评书大全呼家将 刘兰芳评书打包下载 杨家将评书刘兰芳全集下载 评书呼家将刘兰芳 全集 刘兰芳评书下载mp3打包下载 刘兰芳评书呼家将征西 刘兰芳评书大全杨家将 刘兰芳评书岳飞传全集mp3