在Java中,main方法之后的那些语句的初始化顺序是什么 [英] In Java, what is the order of initialization for those statements after main method

查看:158
本文介绍了在Java中,main方法之后的那些语句的初始化顺序是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到初始化顺序的原则是:

I learned that the principle for order of initialization is:


  1. 超类首先(本例中未讨论)

  2. 出现顺序的静态变量声明和静态初始化块

  3. 出现顺序的实例变量声明和静态初始化块

  4. 构造函数

  1. Superclass first (not discussed here in this case)
  2. Static variable declarations and static initialization blocks in the order of appearance
  3. Instance variable declarations and static initialization blocks in the order of appearance
  4. The constructor

但我仍然对此代码的输出感到困惑:

But I'm still confused by the output of this code:

public class Test1 {

    static {
        add(2);
    }

    static void add (int num) {
        System.out.println(num + " ");
    }

    public Test1() {
        add(5);
        System.out.println("Constructor!");
    }

    static {
        add(4);
    }

    {
        add(6);
    }

    static {
        new Test1();
    }

    {
        add(8);
    }

    public static void main(String[] args) {
        System.out.println("Main method!");
        add(10);
    }

    {
        add(11);
    }
    static {
        add(12);
    }
}

结果是:

2 
4 
6 
8 
11 
5 
Constructor!
12
Main method!
10 

如果没有add(10)的陈述;加(11);加(12);我完全可以理解。你能解释一下这3个语句的初始化顺序吗?

If without the statements of add(10); add(11); add(12); I can totally understand. Can you please explain the order of initialization for those 3 statements?

推荐答案

1)调用没有下面这个名字的块实例初始化程序仅在创建新对象时调用,如DefaultConstructor或noArg构造函数。

1) Block that does not have name like below is called "Instance Initializer" which only get called when new objects will be created its like DefaultConstructor or noArg Constructor.

{
    add(11);
}

2)在上面的代码中,您有静态阻止首先在类加载中调用),实例初始化程序在创建对象时调用),显式DefaultConstructor 在创建对象时调用,但始终记住Instance初始化程序优先级)和最后一个Main方法。

2) In above code you have Static Block (Which get called first at the Class Loading itself), Instance Initializer (Which get called while creating the object), Explicit DefaultConstructor (Which get called while creating the object but remember always Instance initializer takes priority) and last Main method.

3)现在让我们分析你的代码,

3) Now lets analyze your code,

第一个电话:

static 
{
   add(2); //print 2
}

第二个电话:

static {
        add(4); // print 4
}

第3次致电:

static {
    new Test1(); 
    // Here Object is getting created so all Instance Initialzer will be called first in a sequential manner.
}

第4个电话:

{
    add(6); // print 6
}

第5个电话:

{
    add(8);  // print 8
}

第6次致电:

{
    add(11);   // print 11
}

第7次致电:实例后将调用Initializer,Explicit Default Constructor。

7th call : After Instance Initializer, Explicit Default Constructor will be called.

public Test1() {
    add(5);    // print 5
    System.out.println("Constructor!");   // print Constructor!
}

第8次调用:最后一个静态块将再次出现被叫。

8th call: Again the last Static block will be called.

static {
    add(12);   // print 12
}

第9次致电:最后main方法将被调用

9th call: Finally the main method will be called

public static void main(String[] args) {
    System.out.println("Main method!");  // print Main method!
    add(10); // print 10
}

这篇关于在Java中,main方法之后的那些语句的初始化顺序是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆