English 中文(简体)
java的条块
原标题:Inline initialization blocks in java

我有一个班子。

public class MyMain{
    public static void main(String... arg){
            Temp t = new Temp(){
                {
                    System.out.println(" instance initialize");
                }
            };

        }
    }

class Temp{
    int i;

    {
        i=9;
        System.out.println("Static"+i);
    }
    Temp(){
        System.out.println("Temp const "+i);
    }
}

在实施主要方法时,产出如下:

Static9
Temp const 9
instance initialize

理想的情况是,这些区块由施工人执行,但线性初步化区在施工者之后被称作。 为什么?

最佳回答

页: 1 对于每一类,任何试剂都在构造机构前执行,但超级则在子类初始化之前先入选。 因此,执行流量是:

  • Initializers in Object
  • Constructor body in Object
  • Initializers in Temp
  • Constructor body in Temp
  • Initializers in anonymous class
  • Constructor body in anonymous class (none)

我强烈建议你重新制定任何象这一方向一样的守则,以便澄清而不是cl。

问题回答

JLS 12.5 规定了施工期间发生事情的顺序(着重部分):

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

(3) This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

(4) Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

(5) Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

简言之,超层建筑商(第3级)在初审人(第4级)之前执行。 两人都是在“该建筑商身体的其余部分”之前被处决的(你没有这方面的例子)。

在匿名类别底级的构造者面前,你在匿名阶级的无隐性构造者一刻和之前便正在构造。

What you are actually creating is not a Temp class instance, but an instance of some anonimous class, that inherits from Temp.

Thus, at first, the Temp initializers are called (anonimous block inside Temp and its construstor) and initializers in anonimous class are called afterwards.

您的守则

Temp t = new Temp(){
    {
        System.out.println(" instance initialize");
    }
};

您正在制作匿名物品,该物品可延伸至: 温度级。

3. 创建子宫:

initialize block from Superclass
constructor of Superclass
initialize block from Subclass
constructor of Subclass

第1点:为了明确起见,你有两个试算器:一个是温度级,另一个是采用主要()方法制作的匿名内质,即温度等级。

Point 2: Instance initializers aren t actually run before constructors. Per the JLS, they re run during a constructor, after delegating to the super constructor and before initializing instance fields and completing the constructor.

第3点:在你的法典中,每个初犯在适当的时间被正确地处决。 我认为,你再次期望第二位执行与第一任同时执行,但那是不正确的,因为正如第1点所指出的,他们为两个不同的班级重新开学。

第4点:你还可能混淆在静态初始执行人和初审人之间。 他们重复了两件不同的事情。

这一目标必须首先记忆,然后才能对其做任何其它事情。 标的是记忆中构造的,然后是你的ole印。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签