JAVA程序执行顺序,你了解了吗?
本文主要介绍以下两块内容的执行顺序,熟悉的大虾可以直接飘过。
一。JAVA中执行顺序
- 静态块
- 块
- 构造器
- 父类构造器
二。JAVA中赋值顺序
- 静态块直接赋值
- 块直接赋值
- 父类继承的属性已赋值
- 静态变量声明时赋值
- 成员变量声明时赋值
- 构造器赋值
第一部分很好测试,我们只需要写一个子类,类中定义一个静态块,一个普通块,一个构造器,它的父类构造器,都打印一条语句,即可明白它们直接的执行顺序
Mastiff类
/**
* 子类藏獒
*/
public class Mastiff extends Dog {
public Mastiff() {
System.out.println("Mastiff");
}
{
System.out.println("block");
}
static {
System.out.println("static block");
}
public static void main(String[] args){
Mastiff mastiff=new Mastiff();
}
}
DOG类
/**
*DOG父类
*/
public class Dog {
public Dog() {
System.out.println("Dog");
}
}
运行结果为:
static block
Dog
block
Mastiff
也就是说,在我们的程序中,实例化一个类对象的时候,运行顺序为:
- 静态块
- 父类构造器
- 本类中的块
- 本类的构造器
我们可以更进一步,如果在父类中也有块和静态块呢?
DOG类改进后源码
/**
*DOG父类
*/
public class Dog {
public Dog() {
System.out.println("Dog");
}
static{
System.out.println("super static block");
}
{
System.out.println("super block");
}
}
Mastiff改进后源码
/**
* 子类藏獒
*/
public class Mastiff extends Dog {
public Mastiff() {
System.out.println("Mastiff");
}
{
System.out.println("block");
}
static {
System.out.println("static block");
}
public static void main(String[] args){
Mastiff mastiff=new Mastiff();
}
}
运行的结果为:
super static block
static block
super block
Dog
block
Mastiff
也就是说此时的运行顺序为:
- 父类静态块
- 自身静态块
- 父类块
- 父类构造器
- 自身块
- 自身构造器
好了,知道了运行的顺序,那么这是为什么呢?
这就要从JVM中类的装载机制和实例化机制开始说起,这里因为主题原因,先不讨论,有兴趣的同学可以自己查资料。
我们再来讨论第二个问题,一个变量的值,它有可能在哪些地方确定呢??
- 从父类继承该值(包括:1.作为父类的成员变量已经赋值 2.在父类的块中赋值 3.在父类的构造器中赋值)
- 在构造器中对其进行赋值
- 在块中进行赋值
- 在方法调用中进行赋值
现在假设在我们刚刚的例子中,有一个变量type,表示狗的品种
/**
*DOG父类
*/
public class Dog {
public String type="父类成员变量赋的值";
public Dog() {
System.out.println("父类构造器--type-->"+type);
type="父类构造器赋的值";
System.out.println("父类构造器----type--->"+type);
}
{
System.out.println("block---type--->"+type);
type="父类块赋的值";
}
}
/**
* 子类藏獒
*/
public class Mastiff extends Dog {
public String type="成员变量赋的值";
public Mastiff() {
System.out.println("构造器---type--->"+type);
type="构造器赋的值";
}
public void say(){
System.out.println("say---type---->"+type);
}
{
System.out.println("block---type--->"+type);
type="块赋的值";
}
public static void main(String[] args){
Mastiff mastiff=new Mastiff();
mastiff.say();
}
}
执行结果如下:
block---type--->父类成员变量赋的值
父类构造器--type-->父类块赋的值
父类构造器----type--->父类构造器赋的值
block---type--->成员变量赋的值
构造器---type--->块赋的值
say---type---->构造器赋的值
答案很明显,赋值顺序为:
- 父类成员变量赋值
- 父类块赋值
- 父类构造器赋值
- 自身成员变量赋值
- 自身块赋值
- 自身构造器赋值
结合我们前面说的程序中的执行顺序,这个显然是很好理解的:
1.成员变量赋值>>>块赋值>>>构造器赋值
2.父类的块>>父类构造器>>自身块>>自身构造器
又因为一个成员变量是不可能在静态变量中赋值的,而且又前面程序执行顺序可知
静态块>>块
所以,程序的赋值步骤为
- 父类的静态变量赋值
- 自身的静态变量赋值
- 父类成员变量赋值
- 父类块赋值
- 父类构造器赋值
- 自身成员变量赋值
- 自身块赋值
- 自身构造器赋值
java程序语言学习教程
http://www.toceansoft.com/
:idea:
第一次发帖哦,不错,虽然基础,但是以前没有注意这些东西。 :idea:
:idea:
self-confidence is very important。 i can speak English fluently too.哈哈哈
不难啊 ,原来 我 太看低自己了 。
呵呵,高人啊,全英文。 恩,谢谢您的体现。 我前天查资料也发现,其实块和声明变量的时候赋值,执行顺序是根据代码的顺序决定的。 如果不是static类型的,则都是分解为两部分,一部分为变量声明,为变量提供内存空间,还有一部分被提取到构造器中进行赋值。 因为忙着6级考试呢。都还没来的及改呢。 感谢高人的提醒,也为误导了大家表示歉意。
1.static and instance invoked sequence
static > instance (always)
2.static initialize and static block
the order as the java source code
similar with static, the order as the java source code
when you new a instance, first initialize the class info(Inheritance Structure tree from top to bottom, and here the static ones will be invoked), and then the instance initialize, and then constructor(still the Inheritance Structure tree from top to bottom).
支持楼主.. :D
:idea: :idea:
很好,很有必要学习一下这些基础的东西。
:? good
谢谢捧场。。。
分析的好!